C# 中小数点四舍五入到最接近的四分之一
c# 中是否有一种简单的方法将小数四舍五入到最近的四分之一 iex0, x.25, x.50 x.75 例如 0.21 将四舍五入为 0.25,5.03 将四舍五入为 5.0
提前感谢您的帮助。
Is there a simple way in c# to round a decimal to the nearest quarter i.e. x.0, x.25, x.50 x.75
for example 0.21 would round to 0.25, 5.03 would round to 5.0
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其乘以四,根据需要将其四舍五入为整数,然后再次除以四:
舍入的各种选项及其解释,可以在这个优秀的答案中找到 此处 :-)
Multiply it by four, round it as you need to an integer, then divide it by four again:
The various options for rounding, and their explanations, can be found in this excellent answer here :-)
或者,您可以使用本博客中给出的 UltimateRoundingFunction:
http://rajputyh.blogspot.in/2014/09/ the-ultimate-rounding-function.html
请拨打以下电话进行标准舍入。即 1.125 将四舍五入为 1.25
调用下面的方法来四舍五入边界值。即 1.125 将四舍五入为 1.00
所谓的“银行家舍入”对于 UltimateRoundingFunction 是不可能的,您必须使用 paxdiablo 的答案来获得该支持:)
Alternatively, you can use UltimateRoundingFunction given in this blog:
http://rajputyh.blogspot.in/2014/09/the-ultimate-rounding-function.html
Call below for standard rounding. i.e. 1.125 will be rounded to 1.25
Call below for rounding down border values. i.e. 1.125 will be rounded to 1.00
So called "Banker's Rounding" is not possible with UltimateRoundingFunction, you have to go with paxdiablo's answer for that support :)
注意:这不是您在脚本中执行的操作。
如果您有这样的情况:
然后转到 Unity >项目设置>时间
然后你可以改变时间。
Notice: This isn't a thing you do in a script.
If you have something like this:
Then go to Unity > Project Settings > Time
And then you can change the time.
基于此答案的扩展方法。
An extension method based on this answer.