java算出十进制中0和1的个数_Java中的十进制数字总和

小编典典

您可能会舍入错误,但是我在这里看不到它。

final double first=198.4;//value extract by unmodifiable format method

final double second=44701.2;//value extract by unmodifiable format method

final double firstDifference= first+second; //I receive 44899.6

final double calculatedDifference=44900.1; // comparison value for the flow

final double error=firstDifference-calculatedDifference;// I receive -0.5

if(Math.abs(error)<=0.5d){

// this branch is entered.

System.out.println(error);

}

版画

-0.5

有两种方法可以更一般地处理此问题。您可以定义一个舍入误差,例如

private static final double ERROR = 1e-9;

if(Math.abs(error)<=0.5d + ERROR){

或使用舍入

final double firstDifference= round(first+second, 1); // call a function to round to one decimal place.

或使用固定精度的整数

final int first=1984;// 198.4 * 10

final int second=447012; // 44701.2 * 10

final int firstDifference= first+second; //I receive 448996

final int calculatedDifference=449001; // comparison value for the flow

final int error=firstDifference-calculatedDifference;// I receive -5

if(Math.abs(error)<=5){

// this branch is entered.

System.out.println(error);

}

或者您可以使用BigDecimal。这通常是许多开发人员的首选解决方案,但恕我直言。;)

2020-10-16

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值