prevent rounding with Number.floatValue() ?

I

iksrazal

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
 
C

Chris Smith

iksrazal said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top