如何在 Java 中将 BigDecimal 转换为 Double?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19650917/
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
How to convert BigDecimal to Double in Java?
提问by Subodh Joshi
How we convert BigDecimal
into Double
in java? I have a requirement where we have to use Double as argument but we are getting BigDecimal
so i have to convert BigDecimal
into Double
.
我们如何在java中转换BigDecimal
成Double
?我有一个要求,我们必须使用 Double 作为参数,但我们得到了,BigDecimal
所以我必须转换BigDecimal
为Double
.
采纳答案by SudoRahul
You need to use the doubleValue()
method to get the double value from a BigDecimal object.
您需要使用该doubleValue()
方法从 BigDecimal 对象中获取双精度值。
BigDecimal bd; // the value you get
double d = bd.doubleValue(); // The double you want
回答by Juned Ahsan
Use doubleValuemethod present in BigDecimalclass :
使用BigDecimal类中的doubleValue方法:
double doubleValue()
Converts this BigDecimal
to a double
.
将其转换BigDecimal
为double
.
回答by HIREN011
You can convert BigDecimal
to double
using .doubleValue()
. But believe me, don't use it if you have currency manipulations. It should always be performed on BigDecimal
objects directly. Precision loss in these calculations are big time problems in currency related calculations.
您可以转换BigDecimal
为double
使用.doubleValue()
. 但是相信我,如果您有货币操纵,请不要使用它。它应该始终BigDecimal
直接在对象上执行。这些计算中的精度损失是货币相关计算中的大问题。