當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigDecimal scaleByPowerOfTen()用法及代碼示例


java.math.BigDecimal.scaleByPowerOfTen(int n)是java上的一個內置方法,該方法返回一個BigDecimal,其數值等於(this * 10n)。結果的小數位是(this.scale() – n)。

用法:

public BigDecimal scaleByPowerOfTen(int n)

參數:
該方法接受整數類型的單個參數n,該參數n表示BigDecimal對象乘以10的冪的值。


返回值:此方法返回值BigBig的對象,其值為* 10n。

以下示例程序旨在說明上述方法:

示例1:

// 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); 
    } 
}
輸出:
754.000 raised to power is 7.54000E+6
75400 raised to power is 7.5400

示例2:

// 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); 
    } 
}
輸出:
200 raised to power is 2.00E+6
7540000000 raised to power is 754000.0000

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



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 BigDecimal scaleByPowerOfTen() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。