سؤال

I am going to make calculations that contain either large or small numbers and result in either a very large or a very small number.

What I am care about now is how to get things go as precise as possible.

When I try this calculation on Google calculator 1000^10 = 1e+30 Which can be read as 1000,000,000,000,000,000,000,000,000,000

When I try the same calculations using BigDecimal, I got a different result!

  BigDecimal bigNumber = new BigDecimal(Math.pow(1000, 10));
  System.out.println(bigNumber);

1000,000,000,000,000,019,884,624,838,656

I am happy that it gives me the full range of numbers without using the scientific notation but it gives some extra rubbish numbers at the end.

Note that I am not confined to BigDecimal. I am care about precision in anyway.

So, How to get a very high precision for a too large number and for a too small number in Java?

هل كانت مفيدة؟

المحلول

Using BigDecimal and doing calculation in double is the same as not using BigDecimal at all.

Use BigDecimal to do calculations:

BigDecimal bigNumber = new BigDecimal(1000).pow(10);
System.out.println(bigNumber);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top