BigDecimal scaleByPowerOfTen() method in Java

The java.math.BigDecimal.scaleByPowerOfTen(int n) is an inbuilt method on java that returns a BigDecimal whose numerical value is equal to (this * 10n). The scale of the result is (this.scale() – n).

Syntax:

public BigDecimal scaleByPowerOfTen(int n)

Parameters:
The method accepts a single parameter n of integer type which refers to the value by which BigDecimal object is multiplied to power of ten.



Return value:This method returns the BigDecimal object of value this * 10n.

Below program illustrates the above mentioned method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// scaleByPowerOfTen() method
  
import java.math.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
  
        // Assign two bigecimal objects
        BigDecimal b1 = new BigDecimal("754.000");
        BigDecimal b2 = new BigDecimal("75400");
  
        // Assign the result of method on b1, b2
        // to BigDecimal objects b3, b4
        BigDecimal b3 = b1.scaleByPowerOfTen(4);
        BigDecimal b4 = b2.scaleByPowerOfTen(-4);
  
        // Print b3, b4 values
        System.out.println(b1 + " raised to power is " + b3);
        System.out.println(b2 + " raised to power is " + b4);
    }
}

chevron_right


Output:

754.000 raised to power is 7.54000E+6
75400 raised to power is 7.5400

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// scaleByPowerOfTen() method
  
import java.math.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
  
        // Assign two bigecimal objects
        BigDecimal b1 = new BigDecimal("200");
        BigDecimal b2 = new BigDecimal("7540000000");
  
        // Assign the result of method on b1, b2
        // to BigDecimal objects b3, b4
        BigDecimal b3 = b1.scaleByPowerOfTen(4);
        BigDecimal b4 = b2.scaleByPowerOfTen(-4);
  
        // Print b3, b4 values
        System.out.println(b1 + " raised to power is " + b3);
        System.out.println(b2 + " raised to power is " + b4);
    }
}

chevron_right


Output:

200 raised to power is 2.00E+6
7540000000 raised to power is 754000.0000

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



My Personal Notes arrow_drop_up

Contented with little yet wishing for more

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.