使用RoR 2.3.8
通常我们有以下代码:
Showing <%= @shops.total_entries %> shops in Canada.
产生结果:
Showing 46 shops in Canada
如果我希望它显示向下舍入值,该怎么办:
Showing 40+ shops in Canada
对于&lt; 10个总条目,它应该显示确切的数字。
三江源。
答案 0 :(得分:2)
def round(total)
total > 10 && total%10 != 0 ? [total/10*10,"+"].join : total.to_s
end
Showing <%= round(@shops.total_entries) %> shops in Canada.
当然最好将其包装为Model方法
class Shop < ActiveRecord::Base
def self.round
total = count
total > 10 && total%10 != 0 ? [total/10*10,"+"].join : total.to_s
# instead of using `[total/10*10,"+"].join` you can use `(total/10*10).to_s+"+"`
end
end
@shops = Shop.were(:region => "Canada")
Showing <%= @shops.round %> shops in Canada.