format - Android DecimalFormat rounding error: Random decimals added -
i'm trying format number. can have 2 6 decimal.
if pass number without decimal, results ok. otherwise adds random decimals.
float value = ...; decimalformat formatter_currency = new decimalformat(); decimalformatsymbols nf = new decimalformatsymbols(); nf.setdecimalseparator('.'); nf.setgroupingseparator(','); formatter_currency.setdecimalformatsymbols(nf); formatter_currency.setmaximumfractiondigits(8); formatter_currency.setminimumfractiondigits(2); return formatter_currency.format(value);
eg: value = 35 -> 35.00
value = 35.6 -> 35.65932558 (expected 35.60)
value = 35.659 -> 35.68899918 (expected 35.659)
i've read problem float variable. i've try double, result same.
how can fix this?
thanks
i wrote quick test program , seems pass of test cases:
import java.text.decimalformat; import java.text.decimalformatsymbols; public class formattest { public static void main(string[] args) { double value = 35.659; decimalformat formatter_currency = new decimalformat(); decimalformatsymbols nf = new decimalformatsymbols(); nf.setdecimalseparator('.'); nf.setgroupingseparator(','); formatter_currency.setdecimalformatsymbols(nf); formatter_currency.setmaximumfractiondigits(8); formatter_currency.setminimumfractiondigits(2); system.out.println(formatter_currency.format(value)); } }
please try compiling on machine , try putting in different values. tried double
instead of float
. please let me know if see same results code posted above.
Comments
Post a Comment