[英]converting decimal to int in c#
例如,当rate = 0.085时,为什么百分比会返回0?
int percentage = (int)rate*100;
The cast operation is applied before the multiplication. 在乘法之前应用强制转换操作。 Try: 尝试:
int percentage = (int)(rate*100);
Edit: Here's the C# guide on order of operator evaluation . 编辑:这是运营商评估顺序的C#指南。
It returns 0 because of order of operations. 由于操作顺序,它返回0。 rate
is cast as an integer prior to multiplying. 在乘法之前将rate
为整数。
You need an extra set of parentheses to make this work. 您需要一组额外的括号才能使其正常工作。
int percentage = (int)(rate*100);
尝试:
int percentage = (int)(rate * 100);
Try: 尝试:
int percentage = (int)(rate*100);
You're converting rate
to an int BEFORE you multiply it otherwise. 你将rate
转换为int,然后再乘以它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.