prevent rounding with Number.floatValue() ?

Discussion in 'Java' started by iksrazal, Jul 3, 2003.

  1. iksrazal

    iksrazal Guest

    I need to have a float in brazil format, such as 1,000 (instead of
    1.000). I also need to guarantee 7 places after the decimal. The value
    is recieved first as a string, avoiding (I hoped) the notorious float
    precision problem. I break the strings with ',', and verify with
    String.length() on the second part. I get the precision I need with:

    Locale locale = new Locale("pt", "BR");
    NumberFormat nf = NumberFormat.getInstance(locale);
    nf.setMinimumFractionDigits(7);

    Number value = nf.parse(_value);

    Doing a println() on 'value' verifies I have the correct format.
    Except I now need to write it to a database column formated as Decimal
    - I cannot give a string to it. Number.floatValue() rounds my numbers
    - I need them in the format I got with println()

    Any ideas?
    iksrazal
     
    iksrazal, Jul 3, 2003
    #1
    1. Advertising

  2. iksrazal

    Chris Smith Guest

    iksrazal wrote:
    > I need to have a float in brazil format, such as 1,000 (instead of
    > 1.000). I also need to guarantee 7 places after the decimal. The value
    > is recieved first as a string, avoiding (I hoped) the notorious float
    > precision problem. I break the strings with ',', and verify with
    > String.length() on the second part. I get the precision I need with:
    >
    > Locale locale = new Locale("pt", "BR");
    > NumberFormat nf = NumberFormat.getInstance(locale);
    > nf.setMinimumFractionDigits(7);
    >
    > Number value = nf.parse(_value);


    Okay. Actually, setMinimumFractionDigits has no effect whatsoever when
    parsing. If you happen to be getting 7 decimal places of accuracy,
    that's entirely a coincidence.

    > Doing a println() on 'value' verifies I have the correct format.


    No, it doesn't. There is NO formatting information associated with a
    floating point number (whether primitive or wrapper type). You're just
    getting lucky.

    > Except I now need to write it to a database column formated as Decimal


    Any what is the type of the database column? Does that type track
    precision? If so, your best bet is to format the number appropriately
    into a String, and feed that in.

    > - I cannot give a string to it. Number.floatValue() rounds my numbers


    Why can't you use a String?

    Number.floatValue will indeed truncate your number to a float, where it
    was probably a double-precision float before. If that's your problem,
    just use doubleValue instead.

    > - I need them in the format I got with println()


    The database is quite unlikely to store numbers in any kind of text
    "format". Nevertheless, the full specification for that format is in
    the API docs for Double.toString(double) and Float.toString(float). You
    can do the conversion yourself where ever you need it done.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 3, 2003
    #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. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    509
    Karl Seguin
    Nov 22, 2005
  2. Sooha Park Lee

    Rounding a floating point number declared in "double"

    Sooha Park Lee, Jul 16, 2003, in forum: C Programming
    Replies:
    2
    Views:
    486
    Mark McIntyre
    Jul 16, 2003
  3. Sooha Park Lee

    Rounding a floating point number declared in "double"

    Sooha Park Lee, Jul 16, 2003, in forum: C Programming
    Replies:
    2
    Views:
    328
    martinh
    Jul 17, 2003
  4. NoKetch

    Rounding to next highest number?

    NoKetch, Dec 15, 2003, in forum: C Programming
    Replies:
    7
    Views:
    587
    Mark McIntyre
    Dec 15, 2003
  5. Dave Uphoff

    Can't prevent rounding in gridbox

    Dave Uphoff, Feb 16, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    113
    Barry
    Feb 18, 2004
Loading...

Share This Page