Java BigDecimal movePointRight() Method with Example

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

BigDecimal Class movePointRight() method

  • movePointRight() method is available in java.math package.
  • movePointRight() 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 right side.
        Resultant BigDecimal = (this BigDecimal) * 10 pow(val)
    
  • movePointRight() 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.
  • movePointRight() method may throw an exception at the time of shifting point to the right.
    ArithmeticException: This exception may throw when the calculated scale is not in a range.

Syntax:

    public BigDecimal movePointRight(int number);

Parameter(s):

  • int number – represents the number of places to shift the decimal point to the right of 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 right.

Example:

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

import java.math.*;

public class MovePointRightOfBD {
    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 right i.e.1654.238
        // so it returns 165423.8 because decimal moves
        // 2 places to the right
        BigDecimal right_shift = b_dec1.movePointRight(2);
        System.out.println("b_dec1.movePointRight(2): " + right_shift);

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

Output

b_dec1.movePointRight(2): 165423.8
b_dec2.movePointRight(-2): 1.0024


Comments and Discussions!

Load comments ↻





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