在 C 中将毫秒转换为秒

标签 c time

一道简单的C题,如何正确简洁地将毫秒转换为秒。 有两个约束:

  • 我在这个微小的 C 编译器子集中没有浮点支持
  • 我需要将秒数四舍五入到最接近的秒数(1-499 毫秒向下舍入,500-999 毫秒向上舍入。不需要关心负值)

    int mseconds = 1600; // should be converted to 2 seconds
    int msec = 23487;  // should be converted to 23 seconds
    

最佳答案

这应该可行

int sec = ((msec + 500) / 1000);

关于在 C 中将毫秒转换为秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1294885/

相关文章:

c - 错误: ‘SIGKILL’ undeclared (first use in this function)

c - pid 0 对于 sched_setaffinity() 意味着什么

ubuntu - Go time.Ticker 在 Ubuntu 16.04 服务器上意外触发

python - 纠正时间序列中的时钟漂移

ruby-on-rails - Ruby on Rails : How to get the current week and loop through it to display its days?

java - 将java中的时间调整为前一小时边界

c++ - 确定重定向套接字打开请求的预期 IP 的 POSIX 调用是什么?

c - 类型定义标准

c - 使用宏 STATIC 的重要性

R:从srt(字幕)文件中提取时间