Java BigDecimal scaleByPowerOfTen()方法

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对象。

以下程序说明了上述方法:

程序1:

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

输出

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 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://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#scaleByPowerOfTen(int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程