We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How can I print a double value 0.0 to 0.00?

vijayam
vijayam asked
on
Medium Priority
372 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.



Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
NumberFormat nf = new DecimalFormat("0.00");
s1 = nf.format(d1.doubleValue());
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
CERTIFIED EXPERT
Top Expert 2016
Commented:
System.out.printf("%.2f\n", d2.doubleValue());

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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 Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT
Commented:
A double/ Double holds the *value*, not the *format*.
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
>> 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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.