博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java BigDecimal setScale()方法与示例
阅读量:2529 次
发布时间:2019-05-11

本文共 4872 字,大约阅读时间需要 16 分钟。

BigDecimal类的setScale()方法 (BigDecimal Class setScale() method)

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.

    这些方法在设置刻度时可能会引发异常。

    • ArithmeticException: This exception may throw when the given parameter holds constants ROUND_UNNECESSARY and the given scaling operation need rounding.ArithmeticException :当给定参数保持常量ROUND_UNNECESSARY且给定缩放操作需要舍入时,可能引发此异常。
    • IllegalArgumentException: This exception may throw when the given parameter r_status is invalid.IllegalArgumentException :当给定参数r_status无效时,可能引发此异常。
  • 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)

    • int sc – represents the new scale value of this BigDecimal.
    • int sc –表示此BigDecimal的新比例值。
  • In the first case, setScale(int sc, int r_status),

    在第一种情况下, setScale(int sc,int r_status)

    • int sc – Similar as defined in the first case.
    • int sc –与第一种情况下定义的相似。
    • int r_status – represents the rounding status.
    • int r_status –表示舍入状态。
  • In the first case, setScale(int sc, RoundingMode rm),

    在第一种情况下, setScale(int sc,RoundingMode rm)

    • int sc – Similar as defined in the first case.
    • int sc –与第一种情况下定义的相似。
    • RoundingMode rm – represents the mode of rounding to implement and it is of "RoundingMode" type.
    • RoundingMode rm –表示要实现的舍入模式,它是“ RoundingMode”类型。

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/

你可能感兴趣的文章
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
RotateCard(自定义旋转view)
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>