Decimal Format

Discussion in 'Java' started by sanna, Jul 7, 2006.

  1. sanna

    sanna Guest

    I am trying to convert a double value to two decimal places, but when I
    enter an amount say 12345678.99000, I get the result as 12345678.99E7
    instead of 12345678.99.
    Here is what my code looks like....

    DecimalFormat df = new DecimalFormat("##############0.00");
    boolean appendO = false;
    StringBuffer buff = new StringBuffer();
    Double amt = new Double(df.format(amount));
    String sAmount = amt.toString();
    String a = sAmount.replace(',',' ');
    StringTokenizer tok = new StringTokenizer(a,".");
    while (tok.hasMoreTokens())
    {
    String token1 = tok.nextToken();
    String token2 = tok.nextToken();
    if (token2.length()<2)
    {
    appendO = true;
    ...................

    Thanks,

    Swiss
     
    sanna, Jul 7, 2006
    #1
    1. Advertising

  2. sanna schrub am Freitag, 7. Juli 2006 15:13 folgendes:

    >
    > I am trying to convert a double value to two decimal
    > places, but when I enter an amount say
    > 12345678.99000, I get the result as 12345678.99E7
    > instead of 12345678.99. Here is what my code looks
    > like....


    Of course you get 12345678.99E7 !

    > DecimalFormat df = new
    > DecimalFormat("##############0.00");
    > boolean appendO = false;
    > StringBuffer buff = new StringBuffer();
    > Double amt = new Double(df.format(amount));


    DecimalFormat df.format returns a String !
    What you are doing is converting the String
    ("12345678.99") back into a double ("12345678.99E7")
    and then again into a String (="12345678.99E7")


    > String sAmount = amt.toString();
    > String a = sAmount.replace(',',' ');
    > StringTokenizer tok = new
    > StringTokenizer(a,"."); while
    > (tok.hasMoreTokens())
    > {
    > String token1 = tok.nextToken();
    > String token2 = tok.nextToken();
    > if (token2.length()<2)


    I really don't know what this is supposed to be ???
    Maybe converting Decimal Separators, which df.format
    already returns according to your locale ? This is not
    necessary, of course.

    So your Code should look like:

    DecimalFormat df = new DecimalFormat("##########0.00");
    String sAmount = df.format(amount)

    This is it, no Black Magic involved...


    greetz Karlheinz Klingbeil (lunqual)
    http://www.lunqual.de http://www.42pixels.de
    http://www.rezeptbuch-pro.de
     
    karlheinz klingbeil, Jul 7, 2006
    #2
    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. shannon
    Replies:
    1
    Views:
    8,603
  2. shannon
    Replies:
    0
    Views:
    574
    shannon
    Feb 1, 2006
  3. Ven
    Replies:
    3
    Views:
    1,331
  4. Gilbert Fine
    Replies:
    8
    Views:
    904
    Zentrader
    Aug 1, 2007
  5. Fei Liu
    Replies:
    21
    Views:
    2,380
    John Bokma
    Dec 16, 2006
Loading...

Share This Page