Ruby Rails BigDecimal 没有正确添加
Ruby on Rails BigDecimal not adding properly
我正在尝试制作一个表格,用户可以在其中输入一个数字 (withdraw/deposit),然后金额将显示在 post 中。但是,无论我尝试什么,BigDecimals 都不会加起来。
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
current_user.update_attributes(deposit: deposits(@micropost.deposit),
withdraw: withdrawals(@micropost.withdraw),
total: totals(@micropost.deposit, @micropost.withdraw))
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def deposits(money)
return current_user.deposit + BigDecimal(money)
end
def withdrawals(money)
return current_user.withdraw + BigDecimal(money)
end
def totals(dep, wit)
return current_user.total + BigDecimal(dep) - BigDecimal(wit)
end
private
def micropost_params
params.require(:micropost).permit(:content, :deposit, :withdraw)
end
end
所以当@micropost.deposit = 2.3 和@micropost.withdraw = 2.4 时,Rails 上的 Ruby 似乎都四舍五入到最接近的整数,输出为 4.0.
这也是我的表格
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<div class="field">
<%= f.text_field :deposit, placeholder: "Deposit. Input only positive numbers, e.g. 0.00" %>
</div>
<div class="field">
<%= f.text_field :withdraw, placeholder: "Withdraw: Input only positive numbers, e.g. 0.00" %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
我认为你有两部分问题:
- BigDecimal - 如果您不希望它四舍五入为整数,则需要精度,因此
BigDecimal(1.7,1)
是 2 而 BigDecimal(1.7,2)
是 1.7
- 取款和存款字段的数据类型可能是
integer
而不是 decimal
我正在尝试制作一个表格,用户可以在其中输入一个数字 (withdraw/deposit),然后金额将显示在 post 中。但是,无论我尝试什么,BigDecimals 都不会加起来。
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
current_user.update_attributes(deposit: deposits(@micropost.deposit),
withdraw: withdrawals(@micropost.withdraw),
total: totals(@micropost.deposit, @micropost.withdraw))
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def deposits(money)
return current_user.deposit + BigDecimal(money)
end
def withdrawals(money)
return current_user.withdraw + BigDecimal(money)
end
def totals(dep, wit)
return current_user.total + BigDecimal(dep) - BigDecimal(wit)
end
private
def micropost_params
params.require(:micropost).permit(:content, :deposit, :withdraw)
end
end
所以当@micropost.deposit = 2.3 和@micropost.withdraw = 2.4 时,Rails 上的 Ruby 似乎都四舍五入到最接近的整数,输出为 4.0.
这也是我的表格
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<div class="field">
<%= f.text_field :deposit, placeholder: "Deposit. Input only positive numbers, e.g. 0.00" %>
</div>
<div class="field">
<%= f.text_field :withdraw, placeholder: "Withdraw: Input only positive numbers, e.g. 0.00" %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
我认为你有两部分问题:
- BigDecimal - 如果您不希望它四舍五入为整数,则需要精度,因此
BigDecimal(1.7,1)
是 2 而BigDecimal(1.7,2)
是 1.7 - 取款和存款字段的数据类型可能是
integer
而不是decimal