本文共 4872 字,大约阅读时间需要 16 分钟。
Syntax:
句法:
public BigDecimal setScale(int sc); public BigDecimal setScale(int sc, int r_status); public BigDecimal setScale(int sc, RoundingMode rm);
setScale() method is available in java.math package.
setScale()方法在java.math包中可用。
setScale(int sc) method is used to set the new scale with the given scale of this BigDecimal object.
setScale(int sc)方法用于使用此BigDecimal对象的给定比例设置新比例。
setScale(int sc, int r_status) method is used to set the new scale with the given scale value, and its non-scale value is calculated by either multiply or divide this BigDecimal non-scaled value by the power of 10.
setScale(int sc,int r_status)方法用于使用给定的标度值设置新标度,其非标度值是通过将此BigDecimal非标度值乘以或除以10的幂来计算的。
setScale(int sc, RoundingMode rm) method is used to set the new scale with the given scale value, and its non-scale value is calculated by either multiply or divide this BigDecimal non-scaled value by the power of 10.
setScale(int sc,RoundingMode rm)方法用于使用给定的标度值设置新标度,其非标度值是通过将此BigDecimal非标度值乘以10的幂来计算的。
These methods may throw an exception at the time of setting a scale.
这些方法在设置刻度时可能会引发异常。
These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, setScale(int sc),
在第一种情况下, setScale(int sc) ,
In the first case, setScale(int sc, int r_status),
在第一种情况下, setScale(int sc,int r_status) ,
In the first case, setScale(int sc, RoundingMode rm),
在第一种情况下, setScale(int sc,RoundingMode rm) ,
Return value:
返回值:
In all the cases, the return type of the method is BigDecimal, it returns the BigDecimal object and its scale value is set by the given parameter and its unscale value is calculated by either divide or multiply this BigDecimal unscaled value by the power of 10.
在所有情况下,该方法的返回类型为BigDecimal ,它返回BigDecimal对象,其缩放比例值由给定参数设置,其不缩放比例值通过将该BigDecimal缩放比例值除以或乘以10的幂来计算。
Example:
例:
// Java program to demonstrate the example // of setScale() method of BigDecimalimport java.math.*;public class SetScaleOfBD { public static void main(String args[]) { // Initialize a variables val String val = "10.3045"; // Initialize a BigDecimal objects BigDecimal b_dec = new BigDecimal(val); System.out.println("scale(): "); // Get old scale int old_scale = b_dec.scale(); System.out.println("b_dec: " + b_dec); System.out.println("b_dec.scale(): " + old_scale); System.out.println(" "); System.out.println("setScale(int): "); // sets the new scale of this // BigDecimal b_dec and scale is the // number of digits represented after // decimal BigDecimal bd = b_dec.setScale(5); // Get new set scale int new_scale = bd.scale(); System.out.println("b_dec: " + bd); System.out.println("b_dec.setScale(5): " + new_scale); System.out.println(" "); System.out.println("setScale(int,int): "); // sets the new scale of this // BigDecimal b_dec with the given rounding // mode "2" bd = b_dec.setScale(3, 2); // Get new set scale new_scale = bd.scale(); System.out.println("b_dec: " + bd); System.out.println("bd.setScale(3,2): " + new_scale); System.out.println(" "); System.out.println("setScale(int,RoundingMode): "); // sets the new scale of this // BigDecimal b_dec with the given RoundingMode // RoundingMode.CEILING bd = b_dec.setScale(3, RoundingMode.CEILING); // Get new set scale new_scale = bd.scale(); System.out.println("b_dec: " + bd); System.out.println("b_dec.setScale(3,RoundingMode.CEILING): " + new_scale); }}
Output
输出量
scale(): b_dec: 10.3045b_dec.scale(): 4 setScale(int): b_dec: 10.30450b_dec.setScale(5): 5 setScale(int,int): b_dec: 10.305bd.setScale(3,2): 3 setScale(int,RoundingMode): b_dec: 10.305b_dec.setScale(3,RoundingMode.CEILING): 3
翻译自:
转载地址:http://asvzd.baihongyu.com/