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

标签 java floating-point bigdecimal

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

最佳答案

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

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

引用自文档:

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.

关于java - 如何在 java 中从 float 转换为 bigDecimal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3829029/

相关文章:

c++ - Cuda编程在float类型方面无法与CPU程序相比具有相同的计算精度

Java BigDecimal 到 String

java - 选择带有字符串的对象包含 Firebase DB 中的一些子字符串

java - 转换器类抛出异常 java.lang.IllegalArgumentException

c# - 使用 LibTIFF.NET C# 将具有浮点像素值的 32 位灰度 Tiff 数组化

java - 将 BigDecimal 显示为整数,同时保留原始值

java - 大数幂运算

java - 数学公式格式没有出现在android mathView中

java - 如何在 Jena 中将 SPARQL 查询转换为 RDF 文件?

python - Decimal Python 与 float 运行时