java 除法后获取数字中的所有小数位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15164636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all decimal places in a number after division
提问by Will Jamieson
I am currently using the BigDecimal and it is giving me more decimals but not nearly enough for what I am trying to do. I need to be able to get all the way to the 10^6 digit. This is my current code
我目前正在使用 BigDecimal,它给了我更多的小数,但对于我想要做的事情来说还不够。我需要能够一直到达 10^6 位。这是我当前的代码
BigDecimal num = new BigDecimal(103993/33102.0);
pw.println(num.toString());
and it outputs 3.14159265301190249175533608649857342243194580078125
它输出3.14159265301190249175533608649857342243194580078125
where the number actually has a lot more decimals: http://www.wolframalpha.com/input/?i=103993%2F33102
数字实际上有更多的小数:http://www.wolframalpha.com/input/?i =103993%2F33102
回答by Eng.Fouad
You are loosing the precision when evaluating this:
评估时,您正在失去精度:
103993/33102.0
as a double division. Actually, the following:
作为双重分裂。其实有以下几点:
BigDecimal num = new BigDecimal(103993/33102.0);
is equivlent to:
相当于:
double d = 103993/33102.0;
BigDecimal num = new BigDecimal(d);
instead, use:
相反,使用:
int scale = 100;
BigDecimal num1 = new BigDecimal(103993);
BigDecimal num2 = new BigDecimal(33102);
System.out.println(num1.divide(num2, scale, RoundingMode.HALF_UP).toString());
OUTPUT:
输出:
3.1415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737
回答by Sean McSomething
The problem is how you are making your number. The 103993/33102.0
is evaluated as a double precision floating point expression (double
) before the BigDecimal
class ever gets involved. You should make separate BigDecimal
objects and use BigDecimal.divide
to get the number you want.
问题是你如何制作你的号码。在涉及该类之前,将103993/33102.0
被评估为双精度浮点表达式 ( double
) BigDecimal
。您应该制作单独的BigDecimal
对象并用于BigDecimal.divide
获取您想要的数字。
Since you want exact results, I'd probably pass both of your numbers in as integers when starting.
由于您想要确切的结果,因此我可能会在开始时将您的两个数字作为整数传递。
Even after doing that, I doubt you'll be able to get out to the 10^6 digit. You might need to implement your own division algorithm to get out to that level as any sane implementation of arbitrary precision math is going to stop long before that point (at least by default).
即使这样做之后,我怀疑您是否能够获得 10^6 位数字。您可能需要实现自己的除法算法才能达到该级别,因为任意精度数学的任何理智实现都将在该点之前很久(至少默认情况下)停止。