?
Solved

float -2 decimal point

Posted on 2002-04-09
6
Medium Priority
?
546 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 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

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

830 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