[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# float -2 decimal point

Posted on 2002-04-09
Medium Priority
545 Views
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
Question by:boyyle
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 9

Accepted Solution

Venci75 earned 200 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

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

LVL 1

Expert Comment

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

LVL 3

Expert Comment

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

LVL 1

Expert Comment

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

Tijo
0

LVL 19

Expert Comment

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

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month12 days, 19 hours left to enroll