aspose file tools
The moose likes Java in General and the fly likes formatting double value Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "formatting double value" Watch "formatting double value" New topic
Author

formatting double value

Sandeep Sirsekar
Greenhorn

Joined: Apr 09, 2008
Posts: 16
hi all
can someone please help me out ,
i want to parse double to precision length of 4 digit after '.'

ive tried

System.out.format("%1$f.4", 12.3456789123456789);
System.out.format("%1$f2.4", 12.3456789123456789);

so can anyone tell me what to do to get

12.3456

thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Use %.4f or %1.4f
The .4 indicates the number of decimals.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
Rob is correct; the problem was that you have the .4 bit after the s.

But you will never get 12.3456 from that number. It will be rounded to 12.3457. You would have to round down, possibly using methods of the BigDecimal and MathContext classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: formatting double value
 
Similar Threads
Assignments .PLS HLP ME !!!!!
Just finished my first java program :-) Can you please give me suggestions on making my code better?
Whats wrong with system.err?
format method
Dividing a double value produces different results