BigDecimal movePointRight() Method in Java

Prerequisite : BigDecimal Basics

The java.math.BigDecimal.movePointRight(int n) method is used to move the decimal point of the current BigDecimal by n places to the right.

  • If n is non-negative, the call merely subtracts n from the scale.
  • If n is negative, the call is equivalent to movePointLeft(-n).

The BigDecimal returned by this method has value (this × 10n) and scale max(this.scale()-n, 0).



Syntax:

public BigDecimal movePointRight(int n)

Parameter: The method takes one parameter n of integer type which refers to the number of places by which the decimal point is required to be moved towards the right.

Return Value: The method returns the same BigDecimal value with the decimal point moved n places to the right.

Exception: The method throws an ArithmeticException if the scale overflows.

Examples:

Input: value = 2300.9856, rightshift = 3
Output: 2300985.6
Explanation:
After shifting the decimal point of 2300.9856 by 3 places to right,
2300985.6 is obtained.
Alternate way: 2300.9856*10^(3)=2300985.6

Input: value = 35001, rightshift = 2
Output: 3500100

Below program illustrate the movePointRight() method of BigDecimal:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to demonstrate movePointRight() method of BigDecimal 
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Create BigDecimal object
        BigDecimal bigdecimal = new BigDecimal("2300.9856");
  
        // Create a int i for decimal right move distance
        int i = 3;
  
        // Call movePointRight() method on BigDecimal by shift i
        BigDecimal changedvalue = bigdecimal.movePointRight(i);
  
        String result = "After applying decimal move right
        by move Distance " + i + " on " + bigdecimal + 
        " New Value is " + changedvalue;
  
        // Print result
        System.out.println(result);
    }
}

chevron_right


Output:

After applying decimal move right by move Distance 3 on 2300.9856 New Value is 2300985.6

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#movePointRight(int)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.