帮我 改一个三元运算表达式,谢谢

evjen 2009-06-10 10:38:38

" var th_psw_p =" + "'" + decimal.Round((decimal)th_psw / (decimal)(th_psw + th_psd + th_psl), 2) * 100 + "%" + "';" +
" var th_psd_p =" + "'" + decimal.Round((decimal)th_psd / (decimal)(th_psw + th_psd + th_psl), 2) * 100 + "%" + "';" +
" var th_psl_p =" + "'" + decimal.Round((decimal)th_psl / (decimal)(th_psw + th_psd + th_psl), 2) * 100 + "%" + "';" +


就是当分母为0的时候 就是0.00

谢谢各位
...全文
24 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
evjen 2009-06-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 LCL_data 的回复:]
引用 7 楼 evjen 的回复:
引用 5 楼 wuyq11 的回复:
decimal.Round((decimal)(th_psw + th_psd + th_psl)==0?0:(decimal)th_psd / (decimal)(th_psw + th_psd + th_psl), 2) * 100


为什么程序运行到这里的时候 还是会报错:试图除以零

???



你这个错误是warning 还是error?
当decimal)(th_psw + th_psd + th_psl)==0时表达式结果返回0啊,,
[/Quote]

是 warning
但是还是觉得奇怪
msnadair 2009-06-11
  • 打赏
  • 举报
回复
(decimal)(th_psw + th_psd + th_psl)=0 ? 0 : ((decimal)th_psw / (decimal)(th_psw + th_psd + th_psl), 2)
wackyboy 2009-06-11
  • 打赏
  • 举报
回复
零不能做除数阿 你将0换成0.00没用阿 要提前判断分母是不是零才行
c02645 2009-06-10
  • 打赏
  • 举报
回复

decimal n=(decimal)(th_psw + th_psd + th_psl);
if(n!=0)
n=decimal.Round((decimal)th_psw / n, 2) * 100

" var th_psw_p =" + "'" + n.ToString() + "%" + "';" +
" var th_psd_p =" + "'" + n.ToString()+ "%" + "';" +
" var th_psl_p =" + "'" + n.ToString() + "%" + "';" +
十八道胡同 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 evjen 的回复:]
引用 5 楼 wuyq11 的回复:
decimal.Round((decimal)(th_psw + th_psd + th_psl)==0?0:(decimal)th_psd / (decimal)(th_psw + th_psd + th_psl), 2) * 100



为什么程序运行到这里的时候 还是会报错:试图除以零

???
[/Quote]

你这个错误是warning 还是error?
当decimal)(th_psw + th_psd + th_psl)==0时表达式结果返回0啊,,
evjen 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
decimal.Round((decimal)(th_psw + th_psd + th_psl)==0?0:(decimal)th_psd / (decimal)(th_psw + th_psd + th_psl), 2) * 100
[/Quote]

为什么程序运行到这里的时候 还是会报错:试图除以零

???
wjq 2009-06-10
  • 打赏
  • 举报
回复
何必写一行呢~分母先算出来放一个变量里,不然用3目运算要计算2次分母
wuyq11 2009-06-10
  • 打赏
  • 举报
回复
decimal.Round((decimal)(th_psw + th_psd + th_psl)==0?0:(decimal)th_psd / (decimal)(th_psw + th_psd + th_psl), 2) * 100
evjen 2009-06-10
  • 打赏
  • 举报
回复
注意 这是3条 并非一条 只是让大家看的清楚点
from911cs 2009-06-10
  • 打赏
  • 举报
回复
up
from911cs 2009-06-10
  • 打赏
  • 举报
回复
up

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧