java - BigDecimal Rounding -
so bigdecimal can't seem store numbers continue infinitely answer 1/3. when tried that, following:
exception in thread "main" java.lang.arithmeticexception: non-terminating decimal expansion; no exact representable decimal result. so decided use roundingmode. when (1/3)*3, want come 1 , not 0.9 or 0.99 or 0.999 or form that. however, if (1/3)*2, don't want round 1.
if divisor 3 can math integers scaled 3 exact results, e.g. use 2 represent 2/3 , use 1 1/3. adding them 3, stands 3/3 = 1.
if need various divisors still work numbers can represented fractions, might want @ question: best way represent fraction in java?
if need represent (and calculate with) irrational numbers, (e.g. pi or sqrt(2)), out of luck. see here: irrational number representation in programming language?
Comments
Post a Comment