clarification regarding DecimalFormat formatting..

R

Rakesh

For a particular application - I need to format decimals with strictly
6 digits , after the decimal separator (even if they are not
significant ).

More specifically I would like the following test case to succeed.

public void testZeroFormatting() {
DecimalFormat myformat = new DecimalFormat("#############.######");
assertEquals("Zero ", "0.000000", myformat.format(0.0f));
}


For now , the function returns 0, instead of 0.000000 (as I want it
to). Any idea what I am missing here.
 
M

Martin Gregorie

For now , the function returns 0, instead of 0.000000 (as I want it to).
Any idea what I am missing here.

DecimalFormat myformat = new DecimalFormat("############0.000000");

....as it says quite clearly in the DecimalFormat documentation.

If for some reason you don't have the JDK documentation installed,
you need to rectify that omission ASAP.
 
S

Stefan Ram

Rakesh said:
I need to format decimals with strictly 6 digits , after
the decimal separator (even if they are not significant ).

public class Main
{
public static void main( final java.lang.String[] args )
{
for( double x = 0; x < 10; ++x )
{
java.lang.System.out.printf( "%.6f%n", java.lang.Math.pow( 263.1, x ));
java.lang.System.out.printf( "%.6f%n", java.lang.Math.pow( 263.1, -x ));
}}}

1,000000
1,000000
263,100000
0,003801
69221,610000
0,000014
18212205,591000
0,000000
4791631290,992102
0,000000
1260678192660,022000
0,000000
331684432488851,800000
0,000000
87266174187816928,000000
0,000000
22959730428814635000,000000
0,000000
6040705075821131000000,000000
0,000000
 
G

GArlington

For a particular application - I need to format decimals with strictly
6 digits , after the decimal separator (even if they are not
significant ).

More specifically I would like the following test case to succeed.

public void testZeroFormatting() {
DecimalFormat myformat = new DecimalFormat("#############.######");
assertEquals("Zero ", "0.000000", myformat.format(0.0f));
}

For now , the function returns 0, instead of 0.000000 (as I want it
to). Any idea what I am missing here.

You need to RTFM.
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html
(Special Pattern Characters)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top