Setting decimal sign ("+" AND "-") in DecimalFormat ?

Discussion in 'Java' started by Mark Sizzler, Feb 21, 2005.

  1. Mark Sizzler

    Mark Sizzler Guest

    How do I specify that a DecimalFormat should ALWAYS prepend the decimal sign?

    I thought DecimalFormat("-00000.00");

    would be ok, But in that case a negative number will start with a double minus:
    --0034.56

    Is there really no other way than

    System.out.println("Number=" + ((number >=0)? "+" : "") + sdf.format(number));

    Mark
     
    Mark Sizzler, Feb 21, 2005
    #1
    1. Advertising

  2. Mark Sizzler

    Roland Guest

    On 21-2-2005 20:17, Mark Sizzler wrote:
    > How do I specify that a DecimalFormat should ALWAYS prepend the decimal sign?
    >
    > I thought DecimalFormat("-00000.00");
    >
    > would be ok, But in that case a negative number will start with a double minus:
    > --0034.56
    >
    > Is there really no other way than
    >
    > System.out.println("Number=" + ((number >=0)? "+" : "") + sdf.format(number));
    >
    > Mark
    >

    Use new DecimalFormat("+00000.00;-00000.00")
    The part following the ';' is the pattern for negative values (and the
    other of course for positive ones). See
    <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>
    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Feb 21, 2005
    #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. Johnny

    DecimalFormat

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

    Padding for DecimalFormat?

    Timo Nentwig, Jan 5, 2004, in forum: Java
    Replies:
    3
    Views:
    4,577
    Tony Dahlman
    Feb 12, 2004
  3. Cliff
    Replies:
    5
    Views:
    553
    Roedy Green
    May 12, 2004
  4. Jimmy
    Replies:
    1
    Views:
    961
    Cowboy \(Gregory A. Beamer\)
    Nov 21, 2006
  5. Jimmy
    Replies:
    3
    Views:
    2,428
    shimmyshack
    Nov 20, 2006
Loading...

Share This Page