clarification regarding DecimalFormat formatting..

Discussion in 'Java' started by Rakesh, Jul 7, 2008.

  1. Rakesh

    Rakesh Guest

    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.
    Rakesh, Jul 7, 2008
    #1
    1. Advertising

  2. On Mon, 07 Jul 2008 14:26:32 -0700, Rakesh wrote:

    > 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.


    --
    martin@ | Martin Gregorie
    gregorie. |
    org | Zappa fan & glider pilot
    Martin Gregorie, Jul 7, 2008
    #2
    1. Advertising

  3. Rakesh

    Stefan Ram Guest

    Rakesh <> writes:
    >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
    Stefan Ram, Jul 7, 2008
    #3
  4. Rakesh

    GArlington Guest

    On Jul 7, 10:26 pm, Rakesh <> wrote:
    > 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)
    GArlington, Jul 8, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnny

    DecimalFormat

    Johnny, Oct 25, 2003, in forum: Java
    Replies:
    10
    Views:
    7,829
    A Dahlman
    Oct 27, 2003
  2. Timo Nentwig

    Padding for DecimalFormat?

    Timo Nentwig, Jan 5, 2004, in forum: Java
    Replies:
    3
    Views:
    4,474
    Tony Dahlman
    Feb 12, 2004
  3. Cliff
    Replies:
    5
    Views:
    527
    Roedy Green
    May 12, 2004
  4. Adam Lipscombe
    Replies:
    2
    Views:
    762
    Virgil Green
    Nov 29, 2004
  5. Mark Sizzler
    Replies:
    1
    Views:
    1,398
    Roland
    Feb 21, 2005
Loading...

Share This Page