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


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