BigDecimal 使用注意点
所属分类 java
浏览量 950
无法整除会报错
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
用 BigDecimal(String val)
BigDecimal bd = new BigDecimal("0.333");
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalTest {
public static void main(String[] args) throws Exception {
BigDecimal bd0 = new BigDecimal(1.0/3);
System.out.println(bd0);
bd0 = bd0.setScale(3, RoundingMode.HALF_UP);
System.out.println(bd0);
bd0 = new BigDecimal("0.333");
System.out.println(bd0);
bd0 = bd0.setScale(2, RoundingMode.HALF_UP);
System.out.println(bd0);
BigDecimal bd1 = new BigDecimal("1");
BigDecimal bd2 = new BigDecimal("2");
BigDecimal bd3 = bd1.divide(bd2);
System.out.println(bd3);
bd2 = new BigDecimal("3");
bd3 = bd1.divide(bd2,3, RoundingMode.HALF_UP);
System.out.println(bd3);
// java.lang.ArithmeticException: Non-terminating decimal expansion; no exact
// representable decimal result.
try {
bd2 = new BigDecimal("3");
bd3 = bd1.divide(bd2);
System.out.println(bd3);
} catch (Throwable e) {
System.out.println(e);
}
}
}
上一篇
下一篇
flink内存管理机制
为何kafka要去掉zookeeper依赖
flink 运行模式 批处理与流处理模式
小巧的Java编译器 Janino
Flink 知识点汇总
Flink Watermark 机制