Bar*_*her 316 ruby currency ruby-on-rails
我正在研究一个非常基本的购物车系统.
我有一个items具有price类型列的表integer.
我无法在包括欧元和美分在内的价格中显示价格值.就Rails框架中的处理货币而言,我是否遗漏了一些明显的东西?
mol*_*olf 485
您可能希望DECIMAL在数据库中使用某种类型.在迁移中,执行以下操作:
# precision is the total number of digits
# scale is the number of digits to the right of the decimal point
add_column :items, :price, :decimal, :precision => 8, :scale => 2
Run Code Online (Sandbox Code Playgroud)
在Rails中,:decimal类型返回为BigDecimal,这对于价格计算很有用.
如果你坚持使用整数,你将不得不手动转换到BigDecimal任何地方,这可能只是一个痛苦.
正如mcl所指出的,要打印价格,请使用:
number_to_currency(price, :unit => "€")
#=> €1,234.01
Run Code Online (Sandbox Code Playgroud)
Ken*_*yer 113
这是一个很好的,简单的方法,利用composed_of(部分ActiveRecord,使用ValueObject模式)和Money gem
你需要
Productinteger,模型(和数据库)中的列:price把它写在你的product.rb文件中:
class Product > ActiveRecord::Base
composed_of :price,
:class_name => 'Money',
:mapping => %w(price cents),
:converter => Proc.new { |value| Money.new(value) }
# ...
Run Code Online (Sandbox Code Playgroud)
你会得到什么:
product.price = "$12.00" 自动转换为Money类product.price.to_s 显示十进制格式的数字("1234.00")product.price.format 显示货币的格式正确的字符串product.price.cents.to_sale*_*dev 25
处理货币的常用做法是使用十进制类型.以下是"使用Rails进行敏捷Web开发"的简单示例
add_column :products, :price, :decimal, :precision => 8, :scale => 2
Run Code Online (Sandbox Code Playgroud)
这将允许您处理从-999,999.99到999,999.99的价格.
您可能还想在您的项目中包含验证
def validate
errors.add(:price, "should be at least 0.01") if price.nil? || price < 0.01
end
Run Code Online (Sandbox Code Playgroud)
理智 - 检查你的价值观.
小智 12
对于一些有抱负的 RoR 开发的初级/初学者来说,只是一点点更新和所有答案的凝聚力,他们肯定会来这里做一些解释。
用于:decimal在数据库中存储资金,正如@molf 建议的那样(以及我的公司在处理资金时使用的黄金标准)。
# precision is the total number of digits
# scale is the number of digits to the right of the decimal point
add_column :items, :price, :decimal, precision: 8, scale: 2
Run Code Online (Sandbox Code Playgroud)
几点:
:decimal将用于BigDecimal解决很多问题。
precision并且scale应该根据您所代表的内容进行调整
如果您处理接收和发送付款,precision: 8并将scale: 2您999,999.99作为最高金额,则在 90% 的情况下都可以。
如果您需要表示财产或稀有汽车的价值,则应使用更高的precision.
如果您使用坐标(经度和纬度),您肯定需要更高的scale.
要使用上述内容生成迁移,请在终端中运行:
bin/rails g migration AddPriceToItems price:decimal{8-2}
Run Code Online (Sandbox Code Playgroud)
或者
bin/rails g migration AddPriceToItems 'price:decimal{5,2}'
Run Code Online (Sandbox Code Playgroud)
如本博文中所述。
KISS额外库再见,使用内置的助手。使用number_to_currency@molf 和@facundofarias 建议。
要number_to_currency在 Rails 控制台中使用helper,请向ActiveSupport'sNumberHelper类发送调用以访问 helper。
例如:
ActiveSupport::NumberHelper.number_to_currency(2_500_000.61, unit: '€', precision: 2, separator: ',', delimiter: '', format: "%n%u")
Run Code Online (Sandbox Code Playgroud)
给出以下输出
2500000,61€
Run Code Online (Sandbox Code Playgroud)
检查其他options的number_to_currency帮手。
您可以将它放在应用程序助手中,并在视图中以任意数量使用它。
module ApplicationHelper
def format_currency(amount)
number_to_currency(amount, unit: '€', precision: 2, separator: ',', delimiter: '', format: "%n%u")
end
end
Run Code Online (Sandbox Code Playgroud)
或者你可以把它Item作为一个实例方法放在模型中,并在你需要格式化价格的地方调用它(在视图或助手中)。
class Item < ActiveRecord::Base
def format_price
number_to_currency(price, unit: '€', precision: 2, separator: ',', delimiter: '', format: "%n%u")
end
end
Run Code Online (Sandbox Code Playgroud)
并且,我如何使用number_to_currency控制器内部的示例(请注意该negative_format选项,用于表示退款)
def refund_information
amount_formatted =
ActionController::Base.helpers.number_to_currency(@refund.amount, negative_format: '(%u%n)')
{
# ...
amount_formatted: amount_formatted,
# ...
}
end
Run Code Online (Sandbox Code Playgroud)
如果你正在使用Postgres(因为我们现在在2017年),你可能想:money尝试给他们的列类型.
add_column :products, :price, :money, default: 0
Run Code Online (Sandbox Code Playgroud)
使用虚拟属性(链接到修订(付费)Railscast),您可以将price_in_cents存储在整数列中,并在产品模型中添加虚拟属性price_in_dollars作为getter和setter.
# Add a price_in_cents integer column
$ rails g migration add_price_in_cents_to_products price_in_cents:integer
# Use virtual attributes in your Product model
# app/models/product.rb
def price_in_dollars
price_in_cents.to_d/100 if price_in_cents
end
def price_in_dollars=(dollars)
self.price_in_cents = dollars.to_d*100 if dollars.present?
end
Run Code Online (Sandbox Code Playgroud)
来源:RailsCasts#016:虚拟属性:虚拟属性是添加不直接映射到数据库的表单字段的简洁方法.在这里,我将展示如何处理验证,关联等.
| 归档时间: |
|
| 查看次数: |
176919 次 |
| 最近记录: |