BigDecimal movePointLeft() Method in Java
prerequisite : BigDecimal Basics
The java.math.BigDecimal.movePointLeft(int n) method is used to move the decimal point of the current BigDecimal by n places to the left.
- If n is non-negative, the call merely adds n to the scale.
- If n is negative, the call is equivalent to movePointRight(-n).
The BigDecimal value returned by this method has value (this × 10-n) and scale max(this.scale()+n, 0).
Syntax:
public BigDecimal movePointLeft(int n)
Parameter: The method takes one parameter n of integer type which refers to the number of places the decimal point is required to be moved towards the left.
Return Value: The method returns the same BigDecimal value with the decimal point moved n places to the left.
Exception: The method throws an ArithmeticException if the scale overflows.
Examples:
Input: value = 2300.9856, Leftshift = 3 Output: 2.3009856 Explanation: while shifting the decimal point of 2300.9856 by 3 places to the left, 2.3009856 is obtained Alternate way: 2300.9856*10^(-3)=2.3009856 Input: value = 35001, Leftshift = 2 Output: 350.01
Below program illustrate the movePointLeft() method of BigDecimal:
// Program to demonstrate movePointLeft() 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 left move distance int i = 3; // Call movePointLeft() method on BigDecimal by shift i // Store the return value as BigDecimal BigDecimal changedvalue = bigdecimal.movePointLeft(i); String result = "After applying decimal move left by move Distance " + i + " on " + bigdecimal + " New Value is " + changedvalue; // Print result System.out.println(result); } } |
After applying decimal move left by move Distance 3 on 2300.9856 New Value is 2.3009856
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#movePointLeft(int)
Recommended Posts:
- BigDecimal min() Method in Java
- BigDecimal ulp() Method in Java
- BigDecimal max() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal plus() method in Java
- BigDecimal toBigIntegerExact() Method in Java
- BigDecimal doubleValue() Method in Java
- BigDecimal byteValueExact() Method in Java
- BigDecimal valueOf() Method in Java
- BigDecimal longValue() Method in Java
- BigDecimal precision() Method in Java
- BigDecimal intValue() Method in Java
- BigDecimal longValueExact() Method in Java
- BigDecimal hashCode() Method in Java
- BigDecimal intvalueExact() Method in Java
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.



