Java中的BigDecimal intValue()方法

这个 JAVA数学大十进制。intValue() 是一个内置函数,用于转换 将BigDecimal转换为整数值。此函数将丢弃函数的任何小数部分 大十进制。如果转换结果太大,无法表示为整数值,则该函数只返回较低阶的32位。

null

语法:

public int intValue()

参数: 此函数不接受任何参数。

返回值: 此函数返回 大十进制。

例如:

Input : 19878124.176
Output : 19878124

Input : "721111"
Output : 721111

下面的程序说明了 JAVA数学大十进制。intValue() 方法:

项目1:

// Java program to illustrate
// intValue() method
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Creating 2 BigDecimal Objects
BigDecimal b1, b2;
// Assigning values to b1, b2
b1 = new BigDecimal( "19878124.176" );
b2 = new BigDecimal( "721111" );
// Displaying their respective Integer Values
System.out.println( "The Integer Value of " + b1 + " is "
+ b1.intValue());
System.out.println( "The Integer Value of " + b2 + " is "
+ b2.intValue());
}
}


输出:

The Integer Value of 19878124.176 is 19878124
The Integer Value of 721111 is 721111

注: 有关大型地震整体震级和精度的信息 BigDecimal值可能会在该函数的转换过程中丢失。因此,可能会返回符号相反的结果。

项目2: 这个程序演示了函数返回带相反符号的结果的情况。

// Java program to illustrate
// intValue() method
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Creating 2 BigDecimal Objects
BigDecimal b1, b2;
// Assigning values to b1, b2
b1 = new BigDecimal( "1987812417600" );
b2 = new BigDecimal( "3567128439701" );
// Displaying their respective Integer Values
System.out.println( "The Integer Value of " + b1 + " is " + b1.intValue());
System.out.println( "The Integer Value of " + b2 + " is " + b2.intValue());
}
}


输出:

The Integer Value of 1987812417600 is -757440448
The Integer Value of 3567128439701 is -1989383275

参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#intValue()

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享