How can I print a double value 0.0 to 0.00?

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.



vijayamAsked:
Who is Participating?
 
CEHJCommented:
System.out.printf("%.2f\n", d2.doubleValue());
0
 
objectsCommented:
NumberFormat nf = new DecimalFormat("0.00");
s1 = nf.format(d1.doubleValue());
0
 
objectsCommented:
0
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.

 
vijayamAuthor Commented:
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
 
CEHJCommented:
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
 
objectsCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
A double/ Double holds the *value*, not the *format*.
0
 
zzynxSoftware engineerCommented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.