Java BigDecimal precision()方法及示例

BigDecimal类precision()方法 (BigDecimal Class precision() method)

  • precision() method is available in java.math package.

    precision()方法在java.math包中可用。

  • precision() method is used to get the precision of this BigDecimal object and we all know the term precision is the number of digits represented in the un-scaled value.

    precision()方法用于获取此BigDecimal对象的精度,我们都知道精度一词是未缩放值中表示的位数。

  • precision() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    precision()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • precision() method does not throw an exception at the time of returning precision.

    precision()方法在返回precision时不会引发异常。

Syntax:

句法:

    public int precision();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is int, it returns the precision of this BigDecimal object.

此方法的返回类型为int ,它返回此BigDecimal对象的精度。

Example:

例:

// Java program to demonstrate the example 
// of int precision() method of BigDecimal

import java.math.*;

public class PrecisionOfBD {
    public static void main(String args[]) {
        // Initialize two variables and
        // first is of "short" and second is  
        // of "String" type
        short val1 = 1321;
        String val2 = "100.24";

        // Initialize two BigDecimal objects  
        BigDecimal b_dec1 = new BigDecimal(val1);
        BigDecimal b_dec2 = new BigDecimal(val2);


        // By using precision() method - is to
        // return the precision of this BigDecimal
        // b_dec1 and precision is the number of
        // digits in unscaled value i.e. 1321 so
        // it returns 4 because total number of 
        // digits in this BigDecimal (1321) is 4
        int precision = b_dec1.precision();
        System.out.println("b_dec1.precision(): " + precision);

        // By using precision() method - is to
        // return the precision of this BigDecimal
        // b_dec2 and precision is the number of
        // digits in unscaled value i.e. 100.24 so
        // it returns 5 because total number of 
        // digits in this BigDecimal (100.24) is 5
        precision = b_dec2.precision();
        System.out.println("b_dec2.precision(): " + precision);
    }
}

Output

输出量

b_dec1.precision(): 4
b_dec2.precision(): 5


翻译自: https://www.includehelp.com/java/bigdecimal-precision-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值