[英]integer division, rounding
There is integer variable, voltage in millivolts.有整数变量,以毫伏为单位的电压。
signed int voltage_mv = 134; //134mV
I have 2-segment display and I want to display hundredths of volts.我有 2 段显示器,我想显示百分之一的伏特。
How can I convert milivolts to hundredths volts in one operation?如何在一次操作中将毫伏转换为百分之一伏? Without IF statement, without function?
没有IF语句,没有函数?
134 => 13
135 => 14
How about simple rounding:简单的四舍五入怎么样:
int millivoltToDisplay (int millivolts)
{
return (millivolts+5)/10;
}
(written as a function for clarity) (为了清楚起见,写成函数)
For the same of completeness, if the denominator is odd, then instead of doing:为了完整性,如果分母为奇数,则不执行:
return (millivolts+denominator/2)/denominator;
you can just have你可以拥有
return (2*millivolts+denominator)/(2*denominator);
and get the correct rounding.并得到正确的舍入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.