java BigDecimal 取整
时间: 2023-10-14 11:02:28
浏览: 109
在Java中,可以使用BigDecimal类来进行精确的数值计算,并且可以对BigDecimal[对象]([http]([http](https://geek.csdn.net/educolumn/68f28ca3131d47699e5902c5d3d74d51?spm=1055.2569.3001.10083)s://geek.csdn.net/educolumn/68f28ca3131d47699e5902c5d3d74d51?spm=1055.2569.3001.10083)s://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083)进行取整操作。有几种方法可以实现BigDecimal的取整:
1. 使用setScale方法:
```java
BigDecimal number = new BigDecimal("3.14159");
BigDecimal roundedNumber = number.setScale(0, RoundingMode.[hal](https://geek.csdn.net/educolumn/04217d9c0abe091a11ecb435337b9eec?spm=1055.2569.3001.10083)F_UP);
System.out.println(roundedNumber); // [输出](https://geek.csdn.net/educolumn/5cd6786a0b73a4d4d27fd638d84e2587?spm=1055.2569.3001.10083): 3
```