Calculating an Answer in Java with more than 16 decimal places

*爱你&永不变心* 提交于 2019-11-29 16:25:28

First of all, the width of a double isn't directly related to the number of decimal places it can hold. A double value is a mantissa and an exponent (among other things), and, of course, is base 2 internally, so it's more complex than "a double can hold X decimal places".

In any case you should be able to use a BigDecimal for all of your calculations, just perform all of your math using the methods of BigDecimal instead of built-in arithmetic operators.

Usage of BigDecimal is good way to solve this issue. No other standard type couldn't handle this precision for you. Other way is to implement your own data structure. But i bet, that BigDecimal will be much much better.

Take a look on Javadoc for BigDecimal (http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html) The main difference between using standard types and this BigDecimal type, that you couldn't use operators like +, -, etc., but you need to use methods like add(),divide(), etc.

BigDecimal is what you want. It has methods like add(), multiply() pow() etc that return new BigDecimal objects.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!