Solved

float -2 decimal point

Posted on 2002-04-09
6
524 Views
Last Modified: 2008-03-10
Hi,
I have this in jsp page.

String a_c = "100.00";
String b_c = "100.55";
float d_f = Float.parseFloat(a_c);
float e_f = Float.parseFloat(b_c);

out.print(d_f + "<br>" + e_f);

The output i get is :
100.0
100.55

My question is how to make the output
100.00
100.55
I mean i want the float always have 2 decimal point.
0
Comment
Question by:boyyle
6 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 50 total points
ID: 6927545
...
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0");
out.print(df.format(d_f) + "<br>" + df.format(e_f));
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6927594
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
0
 
LVL 1

Expert Comment

by:jose_tijo
ID: 6927665
<%
String a_c = "100.00";
String b_c = "100.55";
float d_f = Float.parseFloat(a_c);
float e_f = Float.parseFloat(b_c);

java.text.DecimalFormat decimalFormat = new java.text.DecimalFormat("0.00");

out.print(decimalFormat.format(d_f) + "<br>" + decimalFormat.format(e_f));
%>
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:msterjev
ID: 6927698
jose_tijo, thanks a lot,but we've alredy seen that!
The points are for Venci75!
0
 
LVL 1

Expert Comment

by:jose_tijo
ID: 6927831
Hey
  No probs:) . I was trying to help u a little more

Tijo
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6927913
Alternatively, use the java.math.BigDecimal class. It will represent _exactly_ the value it is asked to hold, not _approximately_ like float or double. If scaling or rounding is necessary it can do that too. And conversion to various primitive types is supported.

Best regards,
Jim Cakalic
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question