I want to convert a string into an integer. The value of the array is 19,99 as a string in my DB column (because of the comma).
我想把一个字符串转换成一个整数。数组的值为19,99作为DB列中的字符串(因为有逗号)。
<% @sales.each do |sale| %>
<% if current_user.id == sale.user_id %>
<% price = Warehouse.where(:product => sale.product).pluck(:mrr) %>
<% price = price.shift.strip.to_i %>
<% agent = current_user.sales.count.to_i %>
<%= value = agent * price %>
<% end %>
<% end %>
current_user.sales.count
is 2
and mrr
is "19,99". I want to multiply those two values but the result is just "38 38".
current_user.sales。计数是2,mrr是19 99。我想把这两个值相乘,结果是38 38。
Does anybody now what to do?
现在有人该怎么办吗?
2 个解决方案
#1
4
'38,38'.split(',').join('.').to_f * 2
#=> 76.76
Another option is using String#sub
(thanks @Stefan!):
另一个选择是使用字符串#sub(感谢@Stefan!):
'38,38'.sub(',', '.').to_f * 2
#=> 76.76
#2
1
Use .to_f
instead of .to_i
to convert it to float. I should work with comma, too.
使用.to_f而不是.to_i将其转换为float。我也应该用逗号。
http://goo.gl/LZmxW4
#1
4
'38,38'.split(',').join('.').to_f * 2
#=> 76.76
Another option is using String#sub
(thanks @Stefan!):
另一个选择是使用字符串#sub(感谢@Stefan!):
'38,38'.sub(',', '.').to_f * 2
#=> 76.76
#2
1
Use .to_f
instead of .to_i
to convert it to float. I should work with comma, too.
使用.to_f而不是.to_i将其转换为float。我也应该用逗号。
http://goo.gl/LZmxW4