Java BigDecimal的setScale()方法及示例

Java BigDecimal的setScale()方法及示例

java.math.BigDecimal .setScale()是用来设置BigDecimal的比例。该方法对当前BigDecimal进行操作,该方法被调用。

在Java中,有三个setScale()方法的重载,如下所示。

  • setScale(int newScale)
  • setScale(int newScale, int roundingMode)
  • setScale(int new Scale, RoundingMode roundingMode)

注意:setScale(int newScale, int roundingMode)从Java 9开始被弃用。

setScale(int newScale)

当保证存在一个指定比例的BigDecimal和正确的值时,该调用通常用于增加比例。如果用户知道BigDecimal的小数部分末尾有足够多的0,可以在不改变其值的情况下重新调整比例,那么该调用也可以用来缩小比例。

语法

public BigDecimal setScale(int newScale)

参数: 该方法接受一个参数newScale,用于设置这个BigDecimal的比例。

返回值: 该方法返回一个比例为指定值的BigDecimal。

异常: 如果指定的比例操作需要四舍五入,则抛出算术异常。

注意: 由于BigDecimal对象是不可改变的,调用该方法不会导致原始对象被修改,setScale返回一个具有正确比例的对象。返回的对象可能是新分配的,也可能不是。

下面的程序用来说明BigDecimal的setScale()方法。

// Java program to demonstrate
// setScale() method of BigDecimal
  
import java.math.BigDecimal;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Object of String created
        // Holds the value
        String input1
            = "31452678569.25";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
  
        // Scale for BigDecimal
        int newScale = 4;
  
        // Using setScale() method
        res = a.setScale(newScale);
  
        // Display the result in BigDecimal
        System.out.println(res);
    }
}

输出。

31452678569.2500

setScale(int newScale, int roundingMode)

本方法用于计算一个BigDecimal,其比例是指定的值,其非比例值是通过将这个BigDecimal的非比例值乘以或除以适当的10次方,以保持其整体值。如果通过操作降低了比例,那么未标定的值必须被除以(而不是乘以)。指定的舍入模式适用于除法。

语法

public BigDecimal setScale(int newScale, int roundingMode)

参数: 该方法接受两个参数newScale,用于设置该BigDecimal的比例,以及roundingMode,用于设置结果的舍入值。

返回值: 该方法返回一个比例为指定值的BigDecimal。

异常: 如果roundingMode是UNNECESSARY,并且指定的比例操作需要舍入,该方法会抛出Arithmetic Exception。如果roundingMode不代表有效的四舍五入模式,该方法还抛出IllegalArgumentException。

下面的程序用来说明BigDecimal的setScale()方法。

示例1:

// Java program to demonstrate
// setScale() method of BigDecimal
  
import java.math.BigDecimal;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Object of String created
        // Holds the value
        String input1
            = "31452678569.24";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
  
        // Scale for BigDecimal
        int newScale = -1;
        try {
  
            // Using setScale() method
            res = a.setScale(newScale, 1);
  
            // Display the result in BigDecimal
            System.out.println(res);
        }
        catch (Exception e) {
  
            // Print Exception
            System.out.println(e);
        }
    }
}

输出。

3.145267856E+10

例2:显示该方法抛出的异常的程序。

// Java program to demonstrate
// setScale() method of BigDecimal
  
import java.math.BigDecimal;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Object of String created
        // Holds the value
        String input1
            = "31452678569.24";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
  
        // Scale for BigDecimal
        int newScale = -1;
  
        try {
  
            // Using setScale() method
            res = a.setScale(newScale, 7);
  
            // Display the result in BigDecimal
            System.out.println(res);
        }
        catch (Exception e) {
  
            // Print Exception
            System.out.println(e);
        }
  
        try {
  
            // Using setScale() method
            res = a.setScale(newScale, 10);
  
            // Display the result in BigDecimal
            System.out.println(res);
        }
        catch (Exception e) {
  
            // Print Exception
            System.out.println(e);
        }
    }
}

输出。

java.lang.ArithmeticException: Rounding necessary
java.lang.IllegalArgumentException: Invalid rounding mode

setScale(int newScale, RoundingMode roundingMode)

本方法用于计算一个BigDecimal,其比例是指定的值,其非比例值是通过将这个BigDecimal的非比例值乘以或除以适当的10次方来保持其整体值。如果通过操作降低了比例,那么未标定的值必须被除以(而不是乘以)。指定的舍入模式适用于除法。
语法

public BigDecimal setScale(int newScale, RoundingMode roundingMode)

参数: 该方法接受两个参数newScale,用于设置该BigDecimal的比例,以及RoundingMode类型的舍入模式,用于告知应用哪种舍入模式。

返回值: 该方法返回一个比例为指定值的BigDecimal。

异常: 如果roundingMode是UNNECESSARY,并且指定的比例操作需要舍入,该方法会抛出算术异常。

下面的程序用来说明BigDecimal的setScale()方法。

例1:

// Java program to demonstrate
// setScale() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Object of String created
        // Holds the value
        String input1
            = "31452678569.24";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
  
        // Scale for BigDecimal
        int newScale = 1;
  
        try {
  
            // Using setScale() method
            // Using RoundingMode.CEILING
            res = a.setScale(newScale, RoundingMode.CEILING);
  
            // Display the result in BigDecimal
            System.out.println(res);
        }
        catch (Exception e) {
  
            // Print Exception
            System.out.println(e);
        }
    }
}

输出。

31452678569.3

例2:显示该方法抛出的异常的程序。

// Java program to demonstrate
// setScale() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Object of String created
        // Holds the value
        String input1
            = "31452678569.24";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
  
        // Scale for BigDecimal
        int newScale = -1;
  
        try {
  
            // Using setScale() method
            // Using RoundingMode.UNNECESSARY
            res = a.setScale(newScale, RoundingMode.UNNECESSARY);
  
            // Display the result in BigDecimal
            System.out.println(res);
        }
        catch (Exception e) {
  
            // Print Exception
            System.out.println(e);
        }
    }
}

输出。

java.lang.ArithmeticException: Rounding necessary

参考文献: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#setScale(int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程