如何防止被零除? [英] How to prevent division by zero?

查看:168
本文介绍了如何防止被零除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 广告= ads.Where(X =>(x.Amount  -  x.Price)/(x.Amount / 100)GT = filter.Persent); 

如果x.Amount == 0我有错误所遇到的零误差鸿沟。



像我这样的要求是为了避免



更新:



这帮助,但我不喜欢这个决定:

 广告= ads.Where (X =>(x.Amount  -  x.Price)/((x.Amount / 100)== 0 0.1:(x.Amount / 100))> = filter.Persent); 



有另一种方式?


解决方案

 广告= ads.Where(X => x.Amount = 0&放大器;&安培;!
(x.Amount - x.Price )/(x.Amount / 100)> = filter.Persent);


ads = ads.Where(x => (x.Amount - x.Price) / (x.Amount / 100) >= filter.Persent);

if x.Amount == 0 I have error "Divide by zero error encountered."

like me in this request is to avoid?

update:

this helped, but I do not like the decision:

ads = ads.Where(x => (x.Amount - x.Price) / ((x.Amount / 100)==0?0.1:(x.Amount / 100)) >= filter.Persent);

there is another way?

解决方案

ads = ads.Where(x => x.Amount != 0 &&
                    (x.Amount - x.Price) / (x.Amount / 100) >= filter.Persent);

这篇关于如何防止被零除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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