java - Dividing two number without losing precision -
i dividing 2 numbers 466489/249001
results in non-terminating decimal expansion.
as
bigdecimal
class stores floating-point numbers practically unlimited precision, have gone it.
bigdecimal dividend = bigdecimal.valueof(466489l); bigdecimal divisor = bigdecimal.valueof(249001l); system.out.println((double)466489/249001); //1.8734422753322275 system.out.println(dividend.divide(divisor, mathcontext.decimal128)); //1.873442275332227581415335681382806
from wolframalpha getting result as
1.87344227533222758141533568138280569154340745619495504034120344898213260187710089517712780269958755185722145694193999220...
so suppose result precise.
now want know how can more precision in result using bigdecimal
calculated wolframalpha?
you should able provide mathcontext
instance custom precision, e.g.
dividend.divide(divisor, new mathcontext( 100 ) ); //whatever precision need
Comments
Post a Comment