标签: ruby rounding
我有一个像31.4032, 31.5266, etc...这样的浮动值。我需要将此值四舍五入到最接近的0.25,如31.00, 31.25, 31.50, 31.75, 32.00, 32.25, 32.50, etc...
31.4032, 31.5266, etc...
31.00, 31.25, 31.50, 31.75, 32.00, 32.25, 32.50, etc...
答案 0 :(得分:2)
首先,将你的数字乘以4,然后将其四舍五入除以4.
>> x = 31.4032 >> (x * 4).round / 4.0 => 31.5