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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:35:49  来源:igfitidea点击:

How to convert BigInteger to BigDecimal?

javabigintegerbigdecimal

提问by Regis

Is there any way to convert a BigIntegerinto a BigDecimal?

有没有办法将 a 转换BigInteger为 a BigDecimal

I know you can go from a BigDecimalto 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

There is a constructorfor that.

有一个构造函数

BigDecimal bigdec = new BigDecimal(bigint);

回答by Pragnesh Modh

public BigDecimal(BigInteger unscaledVal, int scale)Translates a BigIntegerunscaled value and an intscale into a BigDecimal. The value of the BigDecimalis unscaledVal/10^scale.

public BigDecimal(BigInteger unscaledVal, int scale)将未BigInteger缩放的值和int比例转换为BigDecimal. 的价值BigDecimalunscaledVal/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 值。