我在数据库中有钱存储为 cents
通过整数类型。显示时:我将其转换为 float ,因为我想以美元金额显示金额。我总是想用两位数字显示数字:
例如:
5000 => 50.00
500 => 5.00
50 => 0.50
5 => 0.05
0 => 0.00
最难的是获得 50
转换为 0.50
因为它是一个 float ,所以它想转换为 0.5
.
目前行不通的方法:
def cents_to_currency_string
return if cents.nil?
(cents.to_f / 100)
end
最佳答案
利用您自己的实现:
def cents_to_currency_string(cents)
return if cents.nil?
dollars = cents.to_f/100
'%.2f' % dollars
end
关于ruby - 用一位小数显示 float 中的两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40428949/