如何在java中从float转换为bigDecimal?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3829029/
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-14 05:34:42  来源:igfitidea点击:

How to convert from float to bigDecimal in java?

javafloating-pointbigdecimal

提问by akp

How to convert from float to bigDecimal in java?

如何在java中从float转换为bigDecimal?

回答by dogbane

BigDecimal value = new BigDecimal(Float.toString(123.4f));

From the javadocs, the string constructor is generally the preferred way to convert a floatinto a BigDecimal, as it doesn't suffer from the unpredictability of the BigDecimal(double)constructor.

javadocs来看,字符串构造函数通常是将 afloat转换为 BigDecimal的首选方法,因为它不受BigDecimal(double)构造函数不可预测性的影响。

Quote from the docs:

来自文档的引用:

Note: For values other float and double NaN and ±Infinity, this constructor is compatible with the values returned by Float.toString(float) and Double.toString(double). This is generally the preferred way to convert a float or double into a BigDecimal, as it doesn't suffer from the unpredictability of the BigDecimal(double) constructor.

注意:对于 float 和 double NaN 和 ±Infinity 以外的值,此构造函数与 Float.toString(float) 和 Double.toString(double) 返回的值兼容。这通常是将 float 或 double 转换为 BigDecimal 的首选方法,因为它不会受到 BigDecimal(double) 构造函数的不可预测性的影响。

回答by Maurice Perry

For a precision of 3 digits after the decimal point:

对于小数点后 3 位精度:

BigDecimal value = new BigDecimal(f,
        new MathContext(3, RoundingMode.HALF_EVEN));

回答by Spacemonkey

float f = 45.6f;
BigDecimal bd = BigDecimal.valueOf(f);

Quote from documentations:

引用文档:

Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).

注意:这通常是将双精度(或浮点)转换为 BigDecimal 的首选方法,因为返回的值等于从使用 Double.toString(double) 的结果构造一个 BigDecimal 的结果。

Reference: BigDecimal (Java Platform SE 6)

参考:BigDecimal(Java 平台 SE 6)

回答by Johnny Blaze

This is upto my knowledge :

这是据我所知:

   public static BigDecimal floatToBigDecimal(Float a){


    if(a == null || a.isInfinite() || a.isNaN()){
        return BigDecimal.ZERO;
    }
    try{
        return BigDecimal.valueOf(a);

    }catch(Exception e){
        return BigDecimal.ZERO;
    }

}

*Note:This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).

*注意:这通常是将双精度(或浮点数)转换为 BigDecimal 的首选方法,因为返回的值等于从使用 Double.toString(double) 的结果构造一个 BigDecimal 的结果。

public static BigDecimal valueOf(double val)

公共静态 BigDecimal valueOf(double val)

Parameters:
val - double to convert to a BigDecimal.
Returns:
a BigDecimal whose value is equal to or approximately equal to the value of val.
Throws:
NumberFormatException- if val is infinite or NaN.
Since:
1.5

参数
val - 转换为 BigDecimal 的双精度值。
返回
一个 BigDecimal,其值等于或近似等于 val 的值。
抛出:
NumberFormatException- 如果 val 是无穷大或 NaN。
自:
1.5

I have checked whether Infinite or Not a Number, so that there is less chances of NumberFormatException

我已经检查了 Infinite 还是 Not a Number,以便减少 NumberFormatException 的机会