Java BigDecimal movePointLeft()方法与示例

BigDecimal Class max()方法 (BigDecimal Class max() method)

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

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

  • max() method is used to return a BigDecimal which is similar to this BigDecimal with the decimal point shifted to the given number of places to the left side.

    max()方法用于返回与该BigDecimal相似的BigDecimal,其小数点移至左侧的给定位数。

        Resultant BigDecimal = (this BigDecimal) * 10 pow(-val)
    
    
  • max() 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.

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

  • max() method may throw an exception at the time of shifting point to left.

    max()方法在将点向左移动时可能会引发异常。

    ArithmeticException: This exception may throw when the calculated scale is not in a range.

    ArithmeticException :当计算的比例尺不在范围内时,可能引发此异常。

Syntax:

句法:

    public BigDecimal movePointLeft(int number);

Parameter(s):

参数:

  • int number – represents the number of places to shift the decimal point to the left in this BigDecimal.

    int number –表示在此BigDecimal中将小数点向左移动的位数。

Return value:

返回值:

The return type of this method is BigDecimal, it returns the BigDecimal which is similar to this BigDecimal with the decimal point shifted the given (number) of places to the left.

此方法的返回类型为BigDecimal ,它返回与该BigDecimal相似的BigDecimal,其小数点向左移动了给定的位数。

Example:

例:

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

import java.math.*;

public class MovePointLeftOfBD {
    public static void main(String args[]) {
        // Initialize two variables and both
        // are of "“"String" type
        String val1 = "1654.238";
        String val2 = "100.24";

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

        // move the decimal point of the given 
        // number of places to the left i.e.1654.238
        // so it returns 0.1654238 because decimal moves
        // 4 places to the left
        BigDecimal left_shift = b_dec1.movePointLeft(4);
        System.out.println("b_dec1.movePointLeft(4): " + left_shift);

        // move the decimal point of the given 
        // number of places to the left i.e.100.24
        // so it returns 1002.4 but here decimal moves
        // 1 places to the right because the given 
        // parameter is negative
        left_shift = b_dec2.movePointLeft(-1);
        System.out.println("b_dec2.movePointLeft(-1): " + left_shift);
    }
}

Output

输出量

b_dec1.movePointLeft(4): 0.1654238
b_dec2.movePointLeft(-1): 1002.4


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值