简单的C问题,我怎样才能正确并毫不含糊地将毫秒转换为秒.有两个限制:
我需要将秒数四舍五入到最接近的秒数(1-499ms向下舍入,500-999ms向上舍入.不需要关心负值)
int mseconds = 1600; // should be converted to 2 seconds
int msec = 23487; // should be converted to 23 seconds
Run Code Online (Sandbox Code Playgroud)Chi*_*Chi 26
这应该工作
int sec = ((msec + 500) / 1000);
Run Code Online (Sandbox Code Playgroud)
int seconds = msec / 1000;
if (msec % 1000 > 500)
seconds++;
Run Code Online (Sandbox Code Playgroud)