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:
Java
import java.math.*;
public class Gfg {
public static void main(String[] args)
{
BigDecimal b1 = new BigDecimal(" 754.000 ");
BigDecimal b2 = new BigDecimal(" 75400 ");
BigDecimal b3 = b1.scaleByPowerOfTen( 4 );
BigDecimal b4 = b2.scaleByPowerOfTen(- 4 );
System.out.println(b1 + " raised to power is " + b3);
System.out.println(b2 + " raised to power is " + b4);
}
}
|
Output:
754.000 raised to power is 7.54000E+6
75400 raised to power is 7.5400
Program 2:
Java
import java.math.*;
public class Gfg {
public static void main(String[] args)
{
BigDecimal b1 = new BigDecimal(" 200 ");
BigDecimal b2 = new BigDecimal(" 7540000000 ");
BigDecimal b3 = b1.scaleByPowerOfTen( 4 );
BigDecimal b4 = b2.scaleByPowerOfTen(- 4 );
System.out.println(b1 + " raised to power is " + b3);
System.out.println(b2 + " raised to power is " + b4);
}
}
|
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)
Last Updated :
11 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...