📜  Java中的 BigDecimal scaleByPowerOfTen() 方法

📅  最后修改于: 2022-05-13 01:55:10.784000             🧑  作者: Mango

Java中的 BigDecimal scaleByPowerOfTen() 方法

Java.math.BigDecimal.scaleByPowerOfTen( int n )是Java上的一个内置方法,它返回一个 BigDecimal,其数值等于 (this * 10n)。结果的比例是(this.scale() - n)
句法:

public BigDecimal scaleByPowerOfTen(int n)

参数:
该方法接受一个整数类型的参数n ,它指的是 BigDecimal 对象乘以 10 的值。
返回值:该方法返回值为 this * 10n 的 BigDecimal 对象。
下面的程序说明了上述方法:
方案一:

Java
// Java program to demonstrate the
// scaleByPowerOfTen() method
 
import java.math.*;
 
public class Gfg {
 
    public static void main(String[] args)
    {
 
        // Assign two bigdecimal 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);
    }
}


Java
// Java program to demonstrate the
// scaleByPowerOfTen() method
 
import java.math.*;
 
public class Gfg {
 
    public static void main(String[] args)
    {
 
        // Assign two bigdecimal 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);
    }
}


输出:
754.000 raised to power is 7.54000E+6
75400 raised to power is 7.5400

方案二:

Java

// Java program to demonstrate the
// scaleByPowerOfTen() method
 
import java.math.*;
 
public class Gfg {
 
    public static void main(String[] args)
    {
 
        // Assign two bigdecimal 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: Java/math/BigDecimal.html#scaleByPowerOfTen(int)