当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。