Agile Web Development On Rails书中建议金额用integer,单位是cents,好处XX。但是每个金额的field都要写两个method:amount_in_dollar和amount_in_dollar=,不如写个类似于attr_accessor的meta program,一次搞定。两段例子如下:
# config/enviorment.rb
class Module
def amounts_in_yuan(*args)
args.each do |sym|
class_eval %{
def #{sym}_in_yuan
amt = self.#{sym} || 0
sprintf("¥%01.2f", amt.to_f/100)
end
def #{sym}_in_yuan=(amt)
amt.sub!('¥', '') if amt.starts_with?('¥')
self.#{sym} = (amt.to_f * 100).to_i
end
}
end
end
end
# db/migrate/001_create_products.rb
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :price, :integer # 金额定义为integer,单位为分
t.column :cost, :integer
...
end
end
...
end
# app/models/product.rb
class Product < ActiveRecord::Base
amounts_in_yuan :price, :cost
...
end
<%# app/views/products/edit.rhtml %>
...
单价:<%= f.text_field :product, :price_in_yuan %>元
成本:<%= f.text_field :product, :cost_in_yuan %>元
...
分享到:
- 2007-01-23 23:21
- 浏览 2706
- 评论(2)
- 论坛回复 / 浏览 (2 / 3923)
- 查看更多
相关推荐
用于季度业绩预测 压缩包内为python代码
A data structure implemented via a linked list. Useful for finding elements in a small amount of time.
Inspired by the large amount of recent research on column-oriented stores, we have developed a new specialized analytic data engine tightly-coupled with the Tableau data visualization system. The ...
csv表格中为随机森林分类器的测试数据,属性:Disbursed Existing_EMI ID Loan_Amount_Applied Loan_Tenure_Applied Monthly_Income Var4 Var5 Age EMI_Loan_Submitted_Missing Interest_Rate_Missing Loan_Amount_...
}, amount_format: function(amount_part, amount_total) { return amount_part ' / ' amount_total; }, update: $.noop, done: $.noop, fail: $.noop };transition_delay$(document).ready...
image processing code to calculate the amount of mony in image with matlab
WHERE B.CLIENT_CD = W_IN_CLIENT_CD UNION ALL SELECT C.TRD_NO FROM XXXXXXXXX C WHERE C.CLIENT_CD = W_IN_CLIENT_CD ) D WHERE A.TRD_NO = D.TRD_NO AND A.CLIENT_CD = W_IN_CLIENT_CD AND A.RESULT ...
为您提供了以下有关水位的信息: amount_tsh-总静压头(可用于水位的水量) date_recorded-输入行的日期资助者-谁为井提供了资金gps_height-井的高度安装程序-安装井的组织经度-GPS坐标纬度-GPS坐标wpt_name-水位...
amnt funded_amnt funded_amnt_inv term int_rate installment grade sub_grade hardship_payoff_balance_amount hardship_last_payment_amount disbursement_method debt_settlement_flag debt_settlement_flag_...
通过将二维图像展成一维向量进行随机置乱实现图像加解密
将小写金额转成大写,如123.20转成壹佰贰拾叁元两角
Member Function ODCIAggregateTerminate(self In Out strcat_type,ReturnValue Out varchar2,flags in Number) Return Number ); / --2. 创建类型体 Create Or Replace Type Body Strcat_type Is Static ...
坐标系的转换程序,亲测可用~~~~~~~~~~~~~~~~
所有场外基金数据 ts_code name management custodian fund_type found_date due_date list_date issue_date delist_date issue_amount m_fee c_fee duration_year p_value min_amount exp_return benchmark status ...
bulb: 习惯插件 :light_bulb: 习惯插件(事实) :light_bulb: 习惯插件(图表) :person_raising_hand: 简介插件 :calendar: Isocalendar插件 :calendar: Isocalendar插件(全年) :Japanese_monthly_amount_button:...
UPDATE cj_in_out SET cj_in_out.h_discount =dbo.uf_GetStock_discount(h_id) §1.3. 税率更新 更新税率,征求客户意见后全部税率统一为13% --更新批销单主表税率 update yw_px set tax_rate = '.1300' update yw_...
Amount of moisture which sulphuric acid leaves in a gas.pdf
Maximum amount of retries reached爬虫fake_useragent获取ua代理报错教程及资源大全
用c++编的一个个人账户管理系统,希望大家喜欢
ocr_item_amount