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


Java 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 的冪的值。

返回值

此方法返回值為 this * 10 的 BigDecimal 對象n

異常

ArithmeticException− 如果比例超出 32 位整數的範圍。

示例

下麵的例子展示了 math.BigDecimal.scaleByPowerOfTen() 方法的用法。

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 );
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

235.000 raised to 10 power 3 is 235000
23500 raised to 10 power -3 is 23.500

相關用法


注:本文由純淨天空篩選整理自 Java.math.BigDecimal.scaleByPowerOfTen() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。