i have this big problem, i have a input where i format my amounts like this.
15,550.55, the problem is that once i submit that value it store it as 15.50 because of the BigDecimal Object, it is not taking the whole number.
In my Parameters Im recieving that value as "dr_business_remuneration"=>"15,550.55"
The type of data in my postgresql DB is Numeric(19,2) for that attribute
The only thing i want is that once a submit the value be stored as 15550.55, but be shown in my view as 15,550.55.
i tried with a method in my model with a before_save callback, that erase my "," before saving that value.
before_save: montos_formateados
def montos_formateados
if !self.dr_business_remuneration.blank?
self.dr_business_remuneration.sub(",", "").to_f
end
end
but returns this error.
no implicit conversion of String into Integer
CodePudding user response:
There are a few ways to do this. First, if you want it coded to show in terminal correctly, this should work:
'15550.55'.reverse.scan(/.{1,3}/).join(',').reverse
If you're displaying it on a webpage, this is the best:
<%= number_with_delimiter(@number, :delimiter => ',') %>
If you're OK with a few additional dependencies, this is probably the best way:
require 'active_support'
require 'active_support/core_ext/numeric/conversions'
number = 15550.55
number.to_s(:delimited) # => "12,345"
number.to_s(:delimited) # => "12,345.6789"
I hope that helps!