bigdecimal scale()
时间: 2023-11-04 14:06:04 浏览: 30
`BigDecimal`类中的`scale()`方法返回此`BigDecimal`对象的小数位数。如果该数没有小数部分,则返回0。如果有小数部分,则返回小数部分的位数。
例如,对于`BigDecimal("123.45")`,`scale()`方法将返回2,因为它有两个小数位。而对于`BigDecimal("123")`,`scale()`方法将返回0,因为它没有小数部分。
以下是一个示例代码:
```
import java.math.BigDecimal;
public class BigDecimalExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("123");
int scale1 = bd1.scale();
int scale2 = bd2.scale();
System.out.println("Scale of bd1: " + scale1);
System.out.println("Scale of bd2: " + scale2);
}
}
```
输出:
```
Scale of bd1: 2
Scale of bd2: 0
```
相关问题
bigdecimal scale
`BigDecimal`类中的`scale()`方法返回此`BigDecimal`对象的小数位数。如果该数没有小数部分,则返回0。如果有小数部分,则返回小数部分的位数。
例如,对于`BigDecimal("123.45")`,`scale()`方法将返回2,因为它有两个小数位。而对于`BigDecimal("123")`,`scale()`方法将返回0,因为它没有小数部分。
以下是一个示例代码:
```
import java.math.BigDecimal;
public class BigDecimalExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("123");
int scale1 = bd1.scale();
int scale2 = bd2.scale();
System.out.println("Scale of bd1: " + scale1);
System.out.println("Scale of bd2: " + scale2);
}
}
```
输出:
```
Scale of bd1: 2
Scale of bd2: 0
```
bigdecimal.scale()
BigDecimal.scale() 是 Java 中 BigDecimal 类的一个方法,用于返回当前 BigDecimal 对象的小数位数。它返回一个整数,表示小数点右边的位数。
示例用法:
```java
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal number1 = new BigDecimal("123.45");
BigDecimal number2 = new BigDecimal("0.001");
int scale1 = number1.scale(); // 返回2,因为有2位小数
int scale2 = number2.scale(); // 返回3,因为有3位小数
System.out.println("Number 1 scale: " + scale1);
System.out.println("Number 2 scale: " + scale2);
}
}
```
输出:
```
Number 1 scale: 2
Number 2 scale: 3
```
注意:BigDecimal 的小数位数是基于尾随零的。例如,"1.2300" 的小数位数为 4,因为它有 4 个尾随零。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)