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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
NumberFormat nf = new DecimalFormat("0.00");
s1 = nf.format(d1.doubleValue());
Mick BarryJava DeveloperCommented:
CEHJCommented:
System.out.printf("%.2f\n", d2.doubleValue());

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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.
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
Mick BarryJava DeveloperCommented:
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()));
Mayank SAssociate Director - Product EngineeringCommented:
A double/ Double holds the *value*, not the *format*.
zzynxSr. Software 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.