floating point - DecimalFormat weird behavior with pattern #####0.00 in Java -
i using below code snap display float
price value 2 decimal points.
numberformat format = new decimalformat("#####0.00"); float myfloatvalue =\\i able fetch value dynamically string finalprice = format.format(myfloatvalue); // using string (finalprice) export xml purpose.
it seems working fine normally, have noticed examples(given below) not working , produce price more 2 decimal points. not able replicate again, can see in log files.
some examples output of finalprice
string : 0.10999966, 0.1800003, 0.45999908.
can me guess original value of myfloatvalue
these outputs? me replicate scenario , fix it.
the sporadic occurrence makes me wonder whether decimal_format used in several threads concurrently. no-no. 1 expect wrong values too.
maybe order specify fixed locale (decimal point vs. comma, thousand separators).
and float
or double
not suited financial software: numbers approximations.
bigdecimal price = new bigdecimal("9.99"); price = price.multiply(bigdecimal.two); // 19.98 exact
bigdecimal pita writing calculations, keeps precision.
Comments
Post a Comment