BigDecima()的使用:
public class LotteryOdds {
public static void main(String[] args)
{
BigDecimal bd1 = new BigDecimal("22.22222");//BigDecimal(String)
BigDecimal bd2 = new BigDecimal(22.22222); //BigDecimal(double)
BigDecimal bd3 = BigDecimal.valueOf(22.22222);
BigDecimal bd4 = BigDecimal.valueOf(1000 ,2);/*返回值为
x/10的scale幂的一个大实数。*/
System.out.println(bd1); // 22.22222
System.out.println(bd2); // 22.22222000000000008412825991399586200714111328125
System.out.println(bd3);
System.out.println(bd4);
}}
输出结果:
上述结果产生原因可以通过下面函数理解:
BigDecimal(int) // 创建一个具有参数所指定整数值的对象。
BigDecimal(double) // 创建一个具有参数所指定双精度值的对象。参数类型为double的构造方法的结果有一定的不可预知性。不推荐使用
BigDecimal(long) // 创建一个具有参数所指定长整数值的对象。
BigDecimal(String) // 创建一个具有参数所指定以字符串表示的数值的对象。 推荐使用
static Biglnteger.valueOf(long x) //返回值等于 x 的大整数。
static BigDecimal valueOf(long x ,int scale)//返回值为 x / 10的scale幂 的一个大实数。
• Biglnteger.add(Biglnteger other)
• Biglnteger.subtract(Biglnteger other)
• Biglnteger.multipiy(Biginteger other)
• Biglnteger.divide(Biglnteger other)
• Biglnteger.mod(Biglnteger other)
返冋这个大整数和另一个大整数 other的和、 差、 积、 商以及余数。
• int Biglnteger.compareTo(Biglnteger other)
如果这个大整数与另一个大整数 other 相等, 返回 0; 如果这个大整数小于另一个大整数 other, 返回负数; 否则, 返回正数。