将 Java Number 转换为 BigDecimal:最好的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16216248/
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
Convert Java Number to BigDecimal : best way
提问by
I am looking for the best way to convert a Number to a BigDecimal.
我正在寻找将 Number 转换为 BigDecimal 的最佳方法。
Is this good enough?
这够好吗?
Number number;
BigDecimal big = new BigDecimal(number.toString());
Can we lose precision with the toString()
method ?
我们会失去这种toString()
方法的精度吗?
采纳答案by david99world
This is fine, remember that using the constructor of BigDecimal to declare a value can be dangerous when it's not of type String. Consider the below...
这很好,请记住,当值不是 String 类型时,使用 BigDecimal 的构造函数来声明值可能很危险。考虑以下...
BigDecimal valDouble = new BigDecimal(0.35);
System.out.println(valDouble);
This will not print 0.35, it will infact be...
这不会打印 0.35,它实际上是...
0.34999999999999997779553950749686919152736663818359375
I'd say your solution is probably the safest because of that.
因此,我想说您的解决方案可能是最安全的。
回答by jarnbjo
Can we lose precision with toString() method ?
我们可以使用 toString() 方法失去精度吗?
Kind of ... Both Float.toString()
and Double.toString()
only output the number of digits after the decimal separator, which is required for the output uniquely to correspond to a float or double value.
的种类...都Float.toString()
和Double.toString()
仅输出的数字的小数分隔,这是需要的输出唯一地以对应于浮动或双值后的数目。
To use the 0.35 example in david99world's answer, consider the following code:
要在 david99world 的回答中使用 0.35 示例,请考虑以下代码:
BigDecimal bd1 = new BigDecimal(0.35);
Number n = 0.35;
BigDecimal bd2 = new BigDecimal(n.toString());
System.out.println(bd1);
System.out.println(bd2);
An intuitive expectation may be that the two BigDecimal instances are identical, but the output shows that they are not:
一个直观的预期可能是两个 BigDecimal 实例是相同的,但输出显示它们不是:
0.34999999999999997779553950749686919152736663818359375
0.35
The first line is the exact value of the double, since 0.35 cannot be represented exactly. The second line is 0.35, since no more fractional digits are required to represent the distinct value. E.g. the statement 0.34999999999999997779553950749686919152736663818359375 == 0.35
will evaluate to true
.
第一行是双精度值的精确值,因为 0.35 无法精确表示。第二行是 0.35,因为不需要更多的小数位来表示不同的值。例如,该语句0.34999999999999997779553950749686919152736663818359375 == 0.35
将评估为true
。
This is actually not a loss of precision when creating the BigDecimal, the uncertainty is already there in your "source" value. The problem is rather that the discrete values possible using e.g. a float or double value as source not necessarily will be represented by the exact equivalent in the BigDecimal instance.
在创建 BigDecimal 时,这实际上并不是精度损失,不确定性已经存在于您的“源”值中。问题在于,可能使用例如 float 或 double 值作为源的离散值不一定会由 BigDecimal 实例中的精确等效项表示。
回答by Marco Sulla
The best way is
最好的办法是
BigDecimal.valueOf(myDouble);
It's the same internally, but it's an overloaded function that works also for long
s and it's optimized for frequently used long
s value.
So it's more standard, simple and easy to remember.
它在内部是相同的,但它是一个重载函数,也适用于long
s,并且针对常用的long
s 值进行了优化。所以它更标准,更简单,更容易记住。