Java.math.BigDecimal.scaleByPowerOfTen() Method



Description

The java.math.BigDecimal.scaleByPowerOfTen(int n) returns a BigDecimal whose numerical value is equal to (this * 10n). The scale of the result is (this.scale() - n).

Declaration

Following is the declaration for java.math.BigDecimal.scaleByPowerOfTen() method.

public BigDecimal scaleByPowerOfTen(int n)

Parameters

n − Value by which BigDecimal object is multiplied to power of ten.

Return Value

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

Exception

ArithmeticException − If the scale would be outside the range of a 32-bit integer.

Example

The following example shows the usage of math.BigDecimal.scaleByPowerOfTen() method.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 4 BigDecimal objects
      BigDecimal bg1, bg2, bg3, bg4;

      bg1 = new BigDecimal("235.000");
      bg2 = new BigDecimal("23500");

      // assign the result of method on bg1, bg2 to bg3, bg4
      bg3 = bg1.scaleByPowerOfTen(3);
      bg4 = bg2.scaleByPowerOfTen(-3);

      String str1 = bg1 + " raised to 10 power 3 is " +bg3;
      String str2 = bg2 + " raised to 10 power -3 is " +bg4;

      // print bg3, bg4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Let us compile and run the above program, this will produce the following result −

235.000 raised to 10 power 3 is 235000
23500 raised to 10 power -3 is 23.500
java_math_bigdecimal.htm
Advertisements