需要将双精度值转换为大整数或长整型值。已尝试使用大整数,但转换后的值与原始值不同。
double doub = 123456789123456789123456789d;
BigDecimal bd = BigDecimal.valueOf(doub);
System.out.println("value=="+bd.toBigInteger());
value==123456789123456790000000000
Expected output: 123456789123456789123456789发布于 2018-04-16 11:28:09
你的第一行就失去了所有的精确度。
double doub = 123456789123456789123456789d;
System.out.println(doub);将打印以下内容:
1.2345678912345679E26这等于你的
123456789123456790000000000原因是123456789123456789123456789123456789不能用双精度64位IEEE754浮点精确表示。因此,您永远不会在第一个地方拥有要转换为BigInteger的值123456789123456789123456789.如果您将值转换为字符串,则可以将其转换为BigInteger,如下所示:
BigInteger b = new BigInteger("123456789123456789123456789");https://stackoverflow.com/questions/49849181
复制相似问题