c函数向下舍入一半
我正在搜索一个提供向下舍入的函数(C 语言)。例如:
1.5 after round half down = 1 1.49999999 after round half down = 1 1.50000001 after round half down = 2
I'm searching a function (in C language) which provide round half down. For example:
1.5 after round half down = 1 1.49999999 after round half down = 1 1.50000001 after round half down = 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以@jtniehof 的答案为基础。
这总是向下舍入一半。
Building on @jtniehof's answer.
This will always round halves down.
查看数学库中的 round 函数。
Have a look to round functions in math library.
惯用的方式:
两个注意事项:
编辑:三个警告......对于负数来说绝对是错误的。如果您正在做任何复杂的事情,一定要查看数学库中的圆形函数,因为它们已经处理了极端情况。但是,如果在有限的输入上需要快速而肮脏的方法,则可以节省链接数学库的时间。
Idiomatic way:
Two caveats:
EDIT: Three caveats...definitely wrong for negative numbers. If you're doing anything at all complicated, definitely do look at the round functions in the math library, since they've handled the corner cases. But if quick-and-dirty is needed on limited input, this saves linking the math library.
尝试
roundf(float)
或round(double)
Try
roundf(float)
orround(double)