舍入到任意除法?
我想根据任意除法进行舍入,例如,我得到一个从 0 到 1 的数字,我想根据除法将其舍入为 48,例如,如果我得到类似 5/96 的东西,我想要 2/48 或 3/48 。有什么好的公式可以做到这一点?
I want to round according to arbitrary divisions, e.g. I get a number from 0 to 1 and I want to round it according to divisions into 48ths, e.g. if I get something like 5/96, i want either 2/48 or 3/48. What's a good formula to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与四舍五入到小数点相同。如果您有
n
个“分区”和数字x
,请执行round(x*n)/n
。在您的示例中,它将是
round((5/96)*48)/48 = round(2.5)/48 = 2/48
round
可以替换为floor
或ceil
,具体取决于您想要的方向。The same as with rounding to decimal signs. If you have
n
'divisions' and numberx
, doround(x*n)/n
.In your example, it'll be
round((5/96)*48)/48 = round(2.5)/48 = 2/48
round
can be replaced withfloor
orceil
, depending on the direction you want.假设整数除法,因此它将向下舍入:
Assumes integer division, so it will round down: