Method to return the localized float formated with x decimals
All you need to do is to copy and paste this code to your config/environment.rb
# Globalize loc_with_decimals for determining a numer x of decimals to return
module Globalize::CoreExtensions::Float
def loc_with_decimals(decimals = 1)
str = format("%0.#{decimals}f",self).to_s
if str =~ /^[\d\.]+$/
if Locale.active?
active_locale = Locale.active
delimiter = active_locale.thousands_sep
decimal = active_locale.decimal_sep
number_grouping_scheme = active_locale.number_grouping_scheme
end
delimiter ||= ','
decimal ||= '.'
number_grouping_scheme ||= :western
int, frac = str.split('.')
number_grouping_scheme == :indian ?
int.gsub!(/(\d)(?=((\d\d\d)(?!\d))|((\d\d)+(\d\d\d)(?!\d)))/) { |match|
match + delimiter} :
int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/) { |match| match + delimiter }
int + decimal + frac
else
str
end
end
end
If you can tweak this code, please do it!
I’m using this code at: http://bielbid.com.br