Java 如何将 BigInteger 转换为 BigDecimal?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3115413/
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 BigInteger to BigDecimal?
提问by Regis
Is there any way to convert a BigInteger
into a BigDecimal
?
有没有办法将 a 转换BigInteger
为 a BigDecimal
?
I know you can go from a BigDecimal
to a BigInteger
, but I can't find a method to go the other way around in Java.
我知道你可以从 aBigDecimal
转到 a BigInteger
,但我找不到在 Java 中反过来的方法。
回答by bdhar
You have a parameterized constructorfor that.
你有一个参数化的构造函数。
BigDecimal(BigInteger val)
BigDecimal(BigInteger val)
回答by erickson
回答by Pragnesh Modh
public BigDecimal(BigInteger unscaledVal, int scale)
Translates a BigInteger
unscaled value and an int
scale into a BigDecimal
. The value of the BigDecimal
is unscaledVal/10^scale
.
public BigDecimal(BigInteger unscaledVal, int scale)
将未BigInteger
缩放的值和int
比例转换为BigDecimal
. 的价值BigDecimal
是unscaledVal/10^scale
。
Parameters:
参数:
unscaledVal
- unscaled value of the BigDecimal
.scale
- scale of the BigDecimal
.
unscaledVal
- 的未缩放值BigDecimal
。scale
- 的规模BigDecimal
。
Throws:
抛出:
NumberFormatException
- scale is negative
NumberFormatException
- 比例为负
Good luck!!!
祝你好运!!!
回答by Aditya
BigInteger I = new BigInteger("123")
BigDecimal bd = BigDecimal.valueOf(I.intValue());
System.out.println("The Value of bd : "+ bd);
This will convert the BigInteger value of I into BigDecimal value.
这会将 I 的 BigInteger 值转换为 BigDecimal 值。