Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaBigDecimal scaleByPowerOfTen() method in Java

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




// Java program to demonstrate the
// scaleByPowerOfTen() method
 
import java.math.*;
 
public class Gfg {
 
    public static void main(String[] args)
    {
 
        // Assign two bigdecimal 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);
    }
}


Output: 

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

 

Program 2: 
 

Java




// Java program to demonstrate the
// scaleByPowerOfTen() method
 
import java.math.*;
 
public class Gfg {
 
    public static void main(String[] args)
    {
 
        // Assign two bigdecimal 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);
    }
}


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)
 

RELATED ARTICLES

Most Popular

Recent Comments