Answers:
(5.65235534).round(2)
#=> 5.65
(5.6).round(2)仅返回5.6
                    sprintf('%.2f', number)是一种神秘却很强大的数字格式化方式。结果始终是一个字符串,但是由于要四舍五入,所以我认为无论如何都是出于演示目的。sprintf几乎可以用任何方式格式化任何数字,甚至更多。
完整的sprintf文档:http : //www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-sprintf
'%.2f' % number至少在我的经验中,更常见的作品也是如此。
                    0.566666666666666四舍五入0.57
                    "%.2f"几轮5下来,而不是了,有没有什么办法解决呢?
                    得到反馈后,原始解决方案似乎无效。这就是为什么将答案更新为建议之一的原因。
def float_of_2_decimal(float_n) 
  float_n.to_d.round(2, :truncate).to_f
end如果您想将小数点后两位取整,则其他答案也可能有效。但是,如果您希望浮点数的前两位小数不取整,那么这些答案将无济于事。
因此,为了获得前两位小数的浮点数,我使用了此技术。在某些情况下不起作用
def float_of_2_decimal(float_n)
  float_n.round(3).to_s[0..3].to_f
end使用5.666666666666666666666666,它将返回5.66而不是四舍五入5.67。希望对别人有帮助
def float_of_2_decimal(float_n)         num = float_n.to_s.split('.')         num[1] = num[1][0..1]         num.join(".").to_f     end  或者可以使用更简单的方法float_n.to_d.round(2, :truncate).to_f
                    11111111111111111.222222222222222输入,第一场演出1.11和第二场演出1.11111111111111e+16
                    试试这个:
module Util
module MyUtil
    def self.redondear_up(suma,cantidad, decimales=0)
        unless suma.present?
            return nil
        end
        if suma>0
            resultado= (suma.to_f/cantidad)
            return resultado.round(decimales)
        end
        return nil
    end
end 
end 要截断小数,我使用了以下代码:
<th><%#= sprintf("%0.01f",prom/total) %><!--1dec,aprox-->
    <% if prom == 0 or total == 0 %>
        N.E.
    <% else %>
        <%= Integer((prom/total).to_d*10)*0.1 %><!--1decimal,truncado-->
    <% end %>
        <%#= prom/total %>
</th>如果要截断为2位小数,应使用 Integr(a*100)*0.01