Java BigDecimal movePointLeft() Method with Example

BigDecimal Class max() method: Here, we are going to learn about the max() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 04, 2020

BigDecimal Class max() method

  • max() method is available in java.math package.
  • 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.
        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() method may throw an exception at the time of shifting point to left.
    ArithmeticException: This exception may throw when the calculated scale is not in a range.

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.

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.

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



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.