biginteger - What variable type can I use to hold huge numbers (30+ digits) in java? -
is there large variable type can use in java store huge numbers (up around forty digits)?
long
's maximum value 9223372036854775807, 19 digits -- not large enough.
i'm trying create calculator can handle large numbers, because nowadays can hold insufficient 10 digits or so, , want want accurate calculations numbers of larger magnitude
edit
thanks answers. can use biginteger
big integers, limit being computer's memory (should sufficient). decimals, i'll use float
^e, @webdaldo suggested, or bigdecimal
(similar biginteger), @kocko suggested.
you can use biginteger class.
biginteger bi1 = new biginteger("637824629384623845238423545642384"); biginteger bi2 = new biginteger("3039768898793547264523745379249934"); biginteger bigsum = bi1.add(bi2); biginteger bigproduct = bi1.multiply(bi2); system.out.println("sum : " + bigsum); system.out.println("product : " + bigproduct);
output:
sum : 3677593528178171109762168924892318
product : 1938839471287900434078965247064711159607977007048190357000119602656
i should mention bigdecimal, excellent amount calculations compare double.
bigdecimal bd = new bigdecimal("123234545.4767"); bigdecimal displayval = bd.setscale(2, roundingmode.half_even); numberformat usdformat = numberformat.getcurrencyinstance(locale.us); system.out.println(usdformat.format(displayval.doublevalue()));
output:
$123,234,545.48
Comments
Post a Comment