百分比计算误差 [英] Percentage Calculation Error

查看:191
本文介绍了百分比计算误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个方案,其中我需要计算重量损失百分比,其中发布的重量以千克和克为单位,接收的重量也以千克和克为单位.因此该方案将变为4个文本框以及一个按钮和一个额外的文本框,以显示损失百分比.谁能帮我这个忙吗?同样,克需要转换为千克.我已经尝试过了,但是每当克数为000时,都会引发错误.

解决方案

我们不做作业:这是有原因的.在这里,您可以考虑自己被告知的内容,并尝试理解它.也可以在那里帮助您的导师识别您的弱点,并将更多的注意力放在补救措施上.

但是您的特定问题很简单:
每克为000时,都会引发错误." 它将-被零除会生成无法在计算机中存储的结果,因此会生成被零除"的异常. .所以先检查一下:

  double 千克=  0 . 0 ;
如果(克!=  0 )
   {
   公斤=克/ 1000 ;
   } 


您需要将对{kg,g}转换为总计 g(比转换为总计kg简单),然后应用百分比公式(例如100 * (issued-received) / issued),请确保除数大于零(因为在任何情况下,但输入错误,都应为零).


Given a Scenario where i need to calculate the loss percentage in weight where the issued weight is in kg and grams and the received weight is also in kg and grams. so the scenario becomes 4 text boxes and a button and an extra text box to display the loss percentage. Can anyone please help me with this? Also the grams need to be converted into kg. I have tried it but whenever the grams is 000,it throws an error.

解决方案

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

But your specific problem is simple:
"whenever the grams is 000,it throws an error."It will - division by zero generates a result that can''t be stroed in a computer, so it generates a "devision by zero" exception. So check first:

double kilos = 0.0;
if (grams != 0)
   {
   kilos = grams / 1000;
   }


You need to convert the pairs {kg,g} into total g (simpler than converting to total kg) and then apply the percent formula (e.g. 100 * (issued-received) / issued), being sure that divisor is greater than zero (as it, in any case but wrong input, should be).


这篇关于百分比计算误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆