分割后得到一个数字中的所有小数位数 [英] Get all decimal places in a number after division

查看:202
本文介绍了分割后得到一个数字中的所有小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用BigDecimal,它给了我更多的小数,但还不够我所要做的。我需要能够一直到10 ^ 6的数字。这是我目前的代码。

pre $ BigDecimal num = new BigDecimal(103993 / 33102.0);
pw.println(num.toString());

,并输出 3.14159265301190249175533608649857342243194580078125



其中的数字实际上有更多的小数: http://www.wolframalpha.com/输入/?i = 103993%2F33102

解决方案



  103993 / 33102.0 



<作为一个双重的部门。其实,下面是:

$ pre $ BigDecimal num = new BigDecimal(103993 / 33102.0);

等于:

  double d = 103993 / 33102.0; 
BigDecimal num = new BigDecimal(d);

改为使用:

  int scale = 100; 
BigDecimal num1 = new BigDecimal(103993);
BigDecimal num2 = new BigDecimal(33102);
System.out.println(num1.divide(num2,scale,RoundingMode.HALF_UP).toString());

输出:

  3。 9613316415926530119026040722614947737296840070086399613316415926530119026040722614947737 


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 num = new BigDecimal(103993/33102.0);
    pw.println(num.toString());

and it outputs 3.14159265301190249175533608649857342243194580078125

where the number actually has a lot more decimals: http://www.wolframalpha.com/input/?i=103993%2F33102

解决方案

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

这篇关于分割后得到一个数字中的所有小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆