Number Formatting of a doiuble to 2 decimal position

Discussion in 'Java' started by stu, Feb 5, 2006.

  1. stu

    stu Guest

    I tried with setMinimumFractionDigits(int) (from
    java.text.NumberFormat) to get 22.22 of 22.222221.

    I tried it on the double variable as well as after the double has been
    converted to string. What am I doing wrong? Here is my code:

    Note that FahrenheitDegree was declared as int.

    Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
    Double cs = celsiusDegree.setMinimumFractionDigits(2);
    String celsiusString = new Double(cs).toString();

    //String cstr = celsiusString.setMinimumFractionDigits(2);

    String message = String.format(FahrenheitString + " degree Fahrenheit =
    %s degree Celsius.", celsiusString);

    //String message = String.format(FahrenheitString + " degree Fahrenheit
    = %s degree Celsius.", cstr);
     
    stu, Feb 5, 2006
    #1
    1. Advertising

  2. stu wrote:
    > I tried with setMinimumFractionDigits(int) (from
    > java.text.NumberFormat) to get 22.22 of 22.222221.
    >
    > I tried it on the double variable as well as after the double has been
    > converted to string. What am I doing wrong? Here is my code:
    >
    > Note that FahrenheitDegree was declared as int.
    >
    > Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
    > Double cs = celsiusDegree.setMinimumFractionDigits(2);
    > String celsiusString = new Double(cs).toString();
    >
    > //String cstr = celsiusString.setMinimumFractionDigits(2);
    >
    > String message = String.format(FahrenheitString + " degree Fahrenheit =
    > %s degree Celsius.", celsiusString);
    >
    > //String message = String.format(FahrenheitString + " degree Fahrenheit
    > = %s degree Celsius.", cstr);
    >


    setMaximumFractionDigits!
     
    Mark Thornton, Feb 5, 2006
    #2
    1. Advertising

  3. stu

    stu Guest

    I should use setMaximumFractionDigits(int) but the same error anyway:
    double cannot be dereferenced.
     
    stu, Feb 5, 2006
    #3
  4. stu wrote:
    > I tried with setMinimumFractionDigits(int) (from
    > java.text.NumberFormat) to get 22.22 of 22.222221.


    Maybe you want setMaximumFractionDigits() ?
     
    Thomas Schodt, Feb 5, 2006
    #4
  5. stu

    stu Guest


    > setMaximumFractionDigits!


    I realized that but both of these have return type void and that's why
    I was getting "the error double cannot be dereenced".

    So far, I don't see any other method to use except
    getMaximumFractionDigits(int). Then I would need to use
    getMaximumIntegerDigits(int) too to get the whole thing. There's got to
    be a better way.
     
    stu, Feb 5, 2006
    #5
  6. stu

    stu Guest

    Thomas Schodt wrote:
    > stu wrote:
    > > I tried with setMinimumFractionDigits(int) (from
    > > java.text.NumberFormat) to get 22.22 of 22.222221.

    >
    > Maybe you want setMaximumFractionDigits() ?


    I realized that but both of these have return type void.

    So I tried getMinimumFractionDigits(int) (nad a similar one, i.e
    getMaximumDigits(int)for fraction parts) which returns int. Then
    concat the two after converting to string? But

    using those methods still say "cannot find symbol".
     
    stu, Feb 5, 2006
    #6
  7. "stu" <> wrote:

    >
    >> setMaximumFractionDigits!

    >
    > I realized that but both of these have return type void and that's why
    > I was getting "the error double cannot be dereenced".
    >
    > So far, I don't see any other method to use except
    > getMaximumFractionDigits(int). Then I would need to use
    > getMaximumIntegerDigits(int) too to get the whole thing. There's got to
    > be a better way.


    The Format isn't going to give you a substring with the fraction digits,
    it's only set up to give you the entire formatted String. Once you have
    that, use indexOf(".") and substring() to get the fraction digits out of
    the string.



    --
    >;k
     
    Mad Bad Rabbit, Feb 5, 2006
    #7
  8. stu

    stu Guest

    Mad Bad Rabbit wrote:
    > "stu" <> wrote:
    >
    > >
    > >> setMaximumFractionDigits!

    > >
    > > I realized that but both of these have return type void and that's why
    > > I was getting "the error double cannot be dereenced".
    > >
    > > So far, I don't see any other method to use except
    > > getMaximumFractionDigits(int). Then I would need to use
    > > getMaximumIntegerDigits(int) too to get the whole thing. There's got to
    > > be a better way.

    >
    > The Format isn't going to give you a substring with the fraction digits,
    > it's only set up to give you the entire formatted String.


    Actually, that's what I needed.

    >Once you have
    > that, use indexOf(".") and substring() to get the fraction digits out of
    > the string.



    >
    >
    >
    > --
    > >;k
     
    stu, Feb 6, 2006
    #8
  9. Hi,

    Please consider using the java.text.DecimalFormat class to format your
    decimal output. Here is an example:

    import java.text.DecimalFormat;

    public class NumberFormat {
    public static void main( String[] args ) {
    double value = 5.94;
    DecimalFormat df = new DecimalFormat( "##.##" );
    System.out.println( "The value is: " + df.format( value ) );
    }
    }

    java.text.DecimalFormat class formats the output based on a wildcard
    pattern that you feed it. These wild cards are:

    # - for a digit, does not show 0 if last
    0 - for a digit
    .. - for the decimal separator
    - - the minus sign
    , - grouping separator
    E - for scientific notation
    % - shows as percentage

    And there are many more. Please see the JavaDocs on
    java.text.DecimalFormat for more information:

    http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

    If you want control over the rounding of the numbers during
    computations, then consider using the java.math.BigDecimal class.

    Hope it helps!
     
    Ranganath Kini, Feb 6, 2006
    #9
  10. In article <>, stu
    took the hamburger, threw it on the grill, and I said "Oh wow"...

    > I tried with setMinimumFractionDigits(int) (from
    > java.text.NumberFormat) to get 22.22 of 22.222221.
    >
    > I tried it on the double variable as well as after the double has been
    > converted to string. What am I doing wrong? Here is my code:
    >
    > Note that FahrenheitDegree was declared as int.
    >
    > Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
    > Double cs = celsiusDegree.setMinimumFractionDigits(2);
    > String celsiusString = new Double(cs).toString();
    >
    > //String cstr = celsiusString.setMinimumFractionDigits(2);
    >
    > String message = String.format(FahrenheitString + " degree Fahrenheit =
    > %s degree Celsius.", celsiusString);
    >
    > //String message = String.format(FahrenheitString + " degree Fahrenheit
    > = %s degree Celsius.", cstr);
    >
    >


    import java.text.*;

    DecimalFormat df = new DecimalFormat("###,#00.00");

    System.out.print(df.format(yourNumber));

    --
    Noodles Jefferson
    mhm31x9 Smeeter#29 WSD#30
    sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

    NP: "Jellyroll" -- Blue Murder

    "Now, technology's getting better all the time and that's fine,
    but most of the time all you need is a stick of gum,
    a pocketknife, and a smile."

    -- Robert Redford "Spy Game"
     
    Noodles Jefferson, Feb 6, 2006
    #10
  11. stu

    stu Guest


    >
    > import java.text.*;
    >
    > DecimalFormat df = new DecimalFormat("###,#00.00");
    >
    > System.out.print(df.format(yourNumber));


    I found that out yesterday. Unfortunately, I spent so much time for
    that little things.
     
    stu, Feb 6, 2006
    #11
  12. stu

    stu Guest

    Ranganath Kini wrote:
    > Hi,
    >
    > Please consider using the java.text.DecimalFormat class to format your
    > decimal output. Here is an example:
    >
    > import java.text.DecimalFormat;
    >
    > public class NumberFormat {
    > public static void main( String[] args ) {
    > double value = 5.94;
    > DecimalFormat df = new DecimalFormat( "##.##" );
    > System.out.println( "The value is: " + df.format( value ) );
    > }
    > }
    >
    > java.text.DecimalFormat class formats the output based on a wildcard
    > pattern that you feed it. These wild cards are:
    >
    > # - for a digit, does not show 0 if last
    > 0 - for a digit
    > . - for the decimal separator
    > - - the minus sign
    > , - grouping separator
    > E - for scientific notation
    > % - shows as percentage
    >
    > And there are many more. Please see the JavaDocs on
    > java.text.DecimalFormat for more information:
    >
    > http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
    >
    > If you want control over the rounding of the numbers during
    > computations, then consider using the java.math.BigDecimal class.


    Thanks for the tips. Does that mean the format mehtod of Decimal Format
    does do rounding?

    >
    > Hope it helps!


    Thanks for the detail explanation. I did figure out soon after my last
    post.
     
    stu, Feb 6, 2006
    #12
  13. stu

    Tony Morris Guest

    "stu" <> wrote in message
    news:...
    > I tried with setMinimumFractionDigits(int) (from
    > java.text.NumberFormat) to get 22.22 of 22.222221.
    >
    > I tried it on the double variable as well as after the double has been
    > converted to string. What am I doing wrong? Here is my code:
    >
    > Note that FahrenheitDegree was declared as int.
    >
    > Double celsiusDegree = (5 * (( (double) FahrenheitDegree - 32)/9) );
    > Double cs = celsiusDegree.setMinimumFractionDigits(2);
    > String celsiusString = new Double(cs).toString();
    >
    > //String cstr = celsiusString.setMinimumFractionDigits(2);
    >
    > String message = String.format(FahrenheitString + " degree Fahrenheit =
    > %s degree Celsius.", celsiusString);
    >
    > //String message = String.format(FahrenheitString + " degree Fahrenheit
    > = %s degree Celsius.", cstr);
    >


    How do I create a String that represents a double (or float) value with only
    2 decimal places?

    http://jqa.tmorris.net/GetQAndA.action?qids=46&showAnswers=true

    --
    Tony Morris
    http://tmorris.net/
     
    Tony Morris, Feb 7, 2006
    #13
  14. You can use the java.text.DecimalFormat class's format method to
    control rounding on the output only.

    If you need rounding for numbers during calculations, I strongly
    recommend you use java.math.BigDecimal.

    Hope it helps!
     
    Ranganath Kini, Feb 7, 2006
    #14
    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. makok
    Replies:
    1
    Views:
    6,881
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Luis Esteban Valencia

    Formatting decimal number

    Luis Esteban Valencia, Jan 12, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    536
    Philip Q [MVP]
    Jan 12, 2005
  3. BoomerangThree
    Replies:
    3
    Views:
    422
    Jacob
    Aug 20, 2004
  4. muss
    Replies:
    13
    Views:
    723
    Vladimir S. Oka
    Mar 27, 2006
  5. Replies:
    0
    Views:
    304
Loading...

Share This Page