【发布时间】:2018-03-09 07:14:51
【问题描述】:
我正在寻找一个可以将数字返回到最接近十的幂(10,100,1000)的rails函数,并且还需要支持0到1之间的数字(0.1、0.01、0.001):
round(9) = 10
round(19) = 10
round(79) = 100
round(812.12) = 1000
round(0.0321) = 0.01
round(0.0921) = 0.1
我在看:Round number down to nearest power of ten
使用字符串长度接受的答案,不能应用于 0 到 1 之间的数字。
更新 Round up to nearest power of 10 这个看起来很棒。但我仍然无法让它在 Rails 中工作。
【问题讨论】:
标签: ruby-on-rails numbers logic decimal rounding