如何在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/