Java.math.BigDecimal.scaleByPowerOfTen()方法實例
java.math.BigDecimal.scaleByPowerOfTen(int n) 返回一個BigDecimal,其數值等於 (this * 10n). 精度的結果是 (this.scale() - n).
聲明
以下是java.math.BigDecimal.scaleByPowerOfTen()方法的聲明
public BigDecimal scaleByPowerOfTen(int n)
參數
- n - 由BigDecimal對象乘以10的冪值
返回值
此方法返回BigDecimal對象 this * 10n 的值
異常
- ArithmeticException - 如果精度是一個32位範圍以外的整數
例子
下面的例子顯示math.BigDecimal.scaleByPowerOfTen()方法的用法
package com.yiibai; 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 ); } }
讓我們編譯和運行上面的程序,這將產生以下結果:
235.000 raised to 10 power 3 is 235000 23500 raised to 10 power -3 is 23.500