我正在为我的系统中的发票制作 PDF,我希望能够在数据库中存储两位小数的数字。我正在使用MoneyRails
gem 来处理货币,我已经设置precision: 10
并scale: 2
在数据库级别(我postgres
用作我的数据库),但我在逗号后只得到 1 个小数位。为什么?
class AddPrecisionToInvoices < ActiveRecord::Migration[5.2]
def self.up
change_column :invoices, :total_net_amount_cents, :decimal, precision: 10, scale: 2, default: 0.00
change_column :invoices, :total_gross_amount_cents, :decimal, precision: 10, scale: 2, default: 0.00
end
def self.down
change_column :invoices, :total_net_amount_cents, :bigint
change_column :invoices, :total_gross_amount_cents, :bigint
end
end
发票.rb
monetize :total_net_amount_cents
monetize :total_gross_amount_cents
在 Rails 控制台中,
invoice.total_gross_amount_cents = Money.new(20_000_00)
invoice.total_gross_amount.to_f #=> 2000.0
是否可以在 DB 中存储两位小数的数字,例如20,000.00
?我不想在视图中显示 PDF,因此我希望能够将数字放入我的数据库中,因为我从前端应用程序的参数中获取它,而无需在视图中进一步格式化它。