處理貨幣/貨幣的最佳方法是什么?我正在研究一個非?;镜馁徫镘囅到y。我有一個items具有price類型列的表integer。我無法在包括歐元和美分在內的價格中顯示價格值。就Rails框架中的處理貨幣而言,我是否遺漏了一些明顯的東西?
3 回答

慕桂英546537
TA貢獻1848條經驗 獲得超10個贊
您可能希望DECIMAL
在數據庫中使用某種類型。在遷移中,執行以下操作:
# precision is the total number of digits# scale is the number of digits to the right of the decimal pointadd_column :items, :price, :decimal, :precision => 8, :scale => 2
在Rails中,:decimal
類型返回為BigDecimal
,這對于價格計算很有用。
如果你堅持使用整數,你將不得不手動轉換到BigDecimal
任何地方,這可能只是一個痛苦。
正如mcl所指出的,要打印價格,請使用:
number_to_currency(price, :unit => "€")#=> €1,234.01

慕絲7291255
TA貢獻1859條經驗 獲得超6個贊
Ruby&Rails的簡單代碼
<%= number_to_currency(1234567890.50) %>OUT PUT => $1,234,567,890.50
- 3 回答
- 0 關注
- 846 瀏覽
添加回答
舉報
0/150
提交
取消