在我的应用程序的 JSON 输出中,我需要将大多数非整数(即浮点数)数值四舍五入到一个小数位,即 1.7777 将显示为 1.8。
我通过猴子修补 Float 的 as_json 方法做到了这一点:
class Float
def as_json(options={})
self.round 1
end
end
我什至不确定这是否是一个好主意,我刚刚做到了,因为我无法确定在我的 RABL 模板中为数值指定格式。
但现在我还需要显示某些值(纬度和经度),而不需要任何舍入:
object @foo
attributes :bar, :latitude, :longitude
如何告诉 RABL 舍入某些值(条形)而不是其他值(纬度、经度)?