• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with printing double value as 0.00 ?  RSS feed

 
viswanath yadav
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,

Can anyone help in printing the double value as 0.00.
The code snippet is like this,


double d=0.00;
System.out.println(d);

The code shown above will print the value as 0.0, but i want to print the double variable as it is 0.00.

I used a code like this ,

DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(d));

But here the double value is converting a format as String, i don't want to print the string value, I want to print the double variable itself to print the 0.00.


Can anybody please help to provide the code.


Thanks in Advance,


A.Viswanath Yadav.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A double doesn't care about formatting - it's about storage only. When you print it, it is converted to a String (using Double's static toString method). With NumberFormat (of which DecimalFormat is a subclass) and java.util.Formatter (used by String.format and all the printf methods) you can modify this behaviour to convert the double to a String in a way you want.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!