Solved

float -2 decimal point

Posted on 2002-04-09
6
511 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now