?
Solved

How can I print a double value 0.0 to 0.00?

Posted on 2006-03-29
10
Medium Priority
?
284 Views
Last Modified: 2012-05-05
Hi

I am trying to print a string value to Double .But i need to print in different format..

I have used the following code

String s1 = "2"
String s2 = ".1"
Double d1 = new Double(s1);
Double d2 = new Double(s2);
System.out.println("First double"+d1+"### Second double "+d2);

Its pirnting
 FirstDouble 2 ### Second double 0.1.

But i want it be printed as
d1 as 2.00 and  d2 as 0.10

How can it be possible?

Thanks.
Vijaya.



0
Comment
Question by:vijayam
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16319291
NumberFormat nf = new DecimalFormat("0.00");
s1 = nf.format(d1.doubleValue());
0
 
LVL 92

Expert Comment

by:objects
ID: 16319296
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 128 total points
ID: 16319307
System.out.printf("%.2f\n", d2.doubleValue());
0
Technology Partners: 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!

 

Author Comment

by:vijayam
ID: 16319393
I am getting the output,but it is as string but i need it as Double.

DecimalFormat df  = new DecimalFormat("0.00");
FieldPosition fp = new FieldPosition (NumberFormat.INTEGER_FIELD);
StringBuffer sb= new StringBuffer();
sb = df.format( d1 , sb, fp );

then its printing sb as 2.00

But i need it to be as Double  that's why  i coverted this to string and parsed..as

Double dd = new Double(sb.toString());

then its printing,same 2 again...what should i do,if i want to print 2.00 as Double object.

thanks.
Vijaya.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16319422
The representation of an object, when materialized as a String, has a varying relation to its binary content. In the case of numbers, or object wrappers of numbers, the binary form is, and should be, distinct and different
0
 
LVL 92

Assisted Solution

by:objects
objects earned 124 total points
ID: 16319423
doubles are floating point values, you cannot format them directly.
you format them in a string as required

System.out.println("First double"+nf.format(d1.doubleValue())+"### Second double "+nf.format(d2.doubleValue()));
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 124 total points
ID: 16319444
A double/ Double holds the *value*, not the *format*.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 124 total points
ID: 16320038
>> A double/ Double holds the *value*, not the *format*.
Exactly.

>> But i need it to be as Double
That's the value
>> then its printing,same 2 again
That's the format

Your request is like saying I have this

      int i = 2;

and I want it to be 0002 instead of 2.

2 is the value, 0002 is the format, just a representation.
Your Double is 2.0, how you represent it is your choice.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 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