我的功能没有按预期工作。目标是将嵌入式设备(带 LCD)上的人类可读的物理单元字符串打印到缓冲区中。例如1234uV
,应显示为+1.234mV
where -100023uV
as -1.000,230 V
。通过可选的正确调整(at)如何正确(快速和安全)实施uV
?
uint8 voltage_string(char* buf, int32 uVolt)
{
static const int32 VOLT = 1000000;
static const int32 MILLIVOLT = 1000;
const int32 V = uVolt / VOLT;
const int32 mV = (uVolt - V*VOLT) / MILLIVOLT;
const int32 uV = (uVolt - V*VOLT - mV*MILLIVOLT);
uint8 n = 0;
if(abs(V) > 0) {
n = sprintf(buf, "%+d", V);
n += sprintf(buf + n, ",%3d", abs(mV));
n += sprintf(buf + n, ".%3d V", abs(uV));
return n;
}
if(abs(mV) > 0) {
n = sprintf(buf, "%+d", mV);
n += sprintf(buf + n, ",%3d mV", abs(mV));
return n;
}
if(abs(uV) > 0) {
n = sprintf(buf, "%+3d uV", uV);
return n;
}
return n;
}