PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/lib/active_support/number_helper.rb

https://bitbucket.org/suenot/rails
Ruby | 527 lines | 177 code | 48 blank | 302 comment | 24 complexity | 83eaa777dd994cf669518ef0db0522eb MD5 | raw file
  1. require 'active_support/core_ext/big_decimal/conversions'
  2. require 'active_support/core_ext/object/blank'
  3. require 'active_support/core_ext/hash/keys'
  4. require 'active_support/i18n'
  5. module ActiveSupport
  6. module NumberHelper
  7. extend self
  8. DECIMAL_UNITS = { 0 => :unit, 1 => :ten, 2 => :hundred, 3 => :thousand, 6 => :million, 9 => :billion, 12 => :trillion, 15 => :quadrillion,
  9. -1 => :deci, -2 => :centi, -3 => :mili, -6 => :micro, -9 => :nano, -12 => :pico, -15 => :femto }
  10. DEFAULT_CURRENCY_VALUES = { :format => "%u%n", :negative_format => "-%u%n", :unit => "$", :separator => ".", :delimiter => ",",
  11. :precision => 2, :significant => false, :strip_insignificant_zeros => false }
  12. STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb]
  13. # Formats a +number+ into a US phone number (e.g., (555)
  14. # 123-9876). You can customize the format in the +options+ hash.
  15. #
  16. # ==== Options
  17. #
  18. # * <tt>:area_code</tt> - Adds parentheses around the area code.
  19. # * <tt>:delimiter</tt> - Specifies the delimiter to use
  20. # (defaults to "-").
  21. # * <tt>:extension</tt> - Specifies an extension to add to the
  22. # end of the generated number.
  23. # * <tt>:country_code</tt> - Sets the country code for the phone
  24. # number.
  25. # ==== Examples
  26. #
  27. # number_to_phone(5551234) # => 555-1234
  28. # number_to_phone("5551234") # => 555-1234
  29. # number_to_phone(1235551234) # => 123-555-1234
  30. # number_to_phone(1235551234, area_code: true) # => (123) 555-1234
  31. # number_to_phone(1235551234, delimiter: ' ') # => 123 555 1234
  32. # number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
  33. # number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
  34. # number_to_phone("123a456") # => 123a456
  35. #
  36. # number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: '.')
  37. # # => +1.123.555.1234 x 1343
  38. def number_to_phone(number, options = {})
  39. return unless number
  40. options = options.symbolize_keys
  41. number = number.to_s.strip
  42. area_code = options[:area_code]
  43. delimiter = options[:delimiter] || "-"
  44. extension = options[:extension]
  45. country_code = options[:country_code]
  46. if area_code
  47. number.gsub!(/(\d{1,3})(\d{3})(\d{4}$)/,"(\\1) \\2#{delimiter}\\3")
  48. else
  49. number.gsub!(/(\d{0,3})(\d{3})(\d{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3")
  50. number.slice!(0, 1) if number.start_with?(delimiter) && !delimiter.blank?
  51. end
  52. str = ''
  53. str << "+#{country_code}#{delimiter}" unless country_code.blank?
  54. str << number
  55. str << " x #{extension}" unless extension.blank?
  56. str
  57. end
  58. # Formats a +number+ into a currency string (e.g., $13.65). You
  59. # can customize the format in the +options+ hash.
  60. #
  61. # ==== Options
  62. #
  63. # * <tt>:locale</tt> - Sets the locale to be used for formatting
  64. # (defaults to current locale).
  65. # * <tt>:precision</tt> - Sets the level of precision (defaults
  66. # to 2).
  67. # * <tt>:unit</tt> - Sets the denomination of the currency
  68. # (defaults to "$").
  69. # * <tt>:separator</tt> - Sets the separator between the units
  70. # (defaults to ".").
  71. # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
  72. # to ",").
  73. # * <tt>:format</tt> - Sets the format for non-negative numbers
  74. # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
  75. # currency, and <tt>%n</tt> for the number.
  76. # * <tt>:negative_format</tt> - Sets the format for negative
  77. # numbers (defaults to prepending an hyphen to the formatted
  78. # number given by <tt>:format</tt>). Accepts the same fields
  79. # than <tt>:format</tt>, except <tt>%n</tt> is here the
  80. # absolute value of the number.
  81. #
  82. # ==== Examples
  83. #
  84. # number_to_currency(1234567890.50) # => $1,234,567,890.50
  85. # number_to_currency(1234567890.506) # => $1,234,567,890.51
  86. # number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506
  87. # number_to_currency(1234567890.506, locale: :fr) # => 1 234 567 890,51
  88. # number_to_currency('123a456') # => $123a456
  89. #
  90. # number_to_currency(-1234567890.50, negative_format: '(%u%n)')
  91. # # => ($1,234,567,890.50)
  92. # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '')
  93. # # => &pound;1234567890,50
  94. # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
  95. # # => 1234567890,50 &pound;
  96. def number_to_currency(number, options = {})
  97. return unless number
  98. options = options.symbolize_keys
  99. currency = translations_for('currency', options[:locale])
  100. currency[:negative_format] ||= "-" + currency[:format] if currency[:format]
  101. defaults = DEFAULT_CURRENCY_VALUES.merge(defaults_translations(options[:locale])).merge!(currency)
  102. defaults[:negative_format] = "-" + options[:format] if options[:format]
  103. options = defaults.merge!(options)
  104. unit = options.delete(:unit)
  105. format = options.delete(:format)
  106. if number.to_f.phase != 0
  107. format = options.delete(:negative_format)
  108. number = number.respond_to?("abs") ? number.abs : number.sub(/^-/, '')
  109. end
  110. format.gsub('%n', self.number_to_rounded(number, options)).gsub('%u', unit)
  111. end
  112. # Formats a +number+ as a percentage string (e.g., 65%). You can
  113. # customize the format in the +options+ hash.
  114. #
  115. # ==== Options
  116. #
  117. # * <tt>:locale</tt> - Sets the locale to be used for formatting
  118. # (defaults to current locale).
  119. # * <tt>:precision</tt> - Sets the precision of the number
  120. # (defaults to 3).
  121. # * <tt>:significant</tt> - If +true+, precision will be the #
  122. # of significant_digits. If +false+, the # of fractional
  123. # digits (defaults to +false+).
  124. # * <tt>:separator</tt> - Sets the separator between the
  125. # fractional and integer digits (defaults to ".").
  126. # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
  127. # to "").
  128. # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
  129. # insignificant zeros after the decimal separator (defaults to
  130. # +false+).
  131. # * <tt>:format</tt> - Specifies the format of the percentage
  132. # string The number field is <tt>%n</tt> (defaults to "%n%").
  133. #
  134. # ==== Examples
  135. #
  136. # number_to_percentage(100) # => 100.000%
  137. # number_to_percentage('98') # => 98.000%
  138. # number_to_percentage(100, precision: 0) # => 100%
  139. # number_to_percentage(1000, delimiter: '.', separator: ,') # => 1.000,000%
  140. # number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
  141. # number_to_percentage(1000, :locale => :fr) # => 1 000,000%
  142. # number_to_percentage('98a') # => 98a%
  143. # number_to_percentage(100, format: '%n %') # => 100 %
  144. def number_to_percentage(number, options = {})
  145. return unless number
  146. options = options.symbolize_keys
  147. defaults = format_translations('percentage', options[:locale])
  148. options = defaults.merge!(options)
  149. format = options[:format] || "%n%"
  150. format.gsub('%n', self.number_to_rounded(number, options))
  151. end
  152. # Formats a +number+ with grouped thousands using +delimiter+
  153. # (e.g., 12,324). You can customize the format in the +options+
  154. # hash.
  155. #
  156. # ==== Options
  157. #
  158. # * <tt>:locale</tt> - Sets the locale to be used for formatting
  159. # (defaults to current locale).
  160. # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
  161. # to ",").
  162. # * <tt>:separator</tt> - Sets the separator between the
  163. # fractional and integer digits (defaults to ".").
  164. #
  165. # ==== Examples
  166. #
  167. # number_to_delimited(12345678) # => 12,345,678
  168. # number_to_delimited('123456') # => 123,456
  169. # number_to_delimited(12345678.05) # => 12,345,678.05
  170. # number_to_delimited(12345678, delimiter: '.') # => 12.345.678
  171. # number_to_delimited(12345678, delimiter: ',') # => 12,345,678
  172. # number_to_delimited(12345678.05, separator: ' ') # => 12,345,678 05
  173. # number_to_delimited(12345678.05, locale: :fr) # => 12 345 678,05
  174. # number_to_delimited('112a') # => 112a
  175. # number_to_delimited(98765432.98, delimiter: ' ', separator: ',')
  176. # # => 98 765 432,98
  177. def number_to_delimited(number, options = {})
  178. options = options.symbolize_keys
  179. return number unless valid_float?(number)
  180. options = defaults_translations(options[:locale]).merge(options)
  181. parts = number.to_s.to_str.split('.')
  182. parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
  183. parts.join(options[:separator])
  184. end
  185. # Formats a +number+ with the specified level of
  186. # <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
  187. # +:significant+ is +false+, and 5 if +:significant+ is +true+).
  188. # You can customize the format in the +options+ hash.
  189. #
  190. # ==== Options
  191. #
  192. # * <tt>:locale</tt> - Sets the locale to be used for formatting
  193. # (defaults to current locale).
  194. # * <tt>:precision</tt> - Sets the precision of the number
  195. # (defaults to 3).
  196. # * <tt>:significant</tt> - If +true+, precision will be the #
  197. # of significant_digits. If +false+, the # of fractional
  198. # digits (defaults to +false+).
  199. # * <tt>:separator</tt> - Sets the separator between the
  200. # fractional and integer digits (defaults to ".").
  201. # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
  202. # to "").
  203. # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
  204. # insignificant zeros after the decimal separator (defaults to
  205. # +false+).
  206. #
  207. # ==== Examples
  208. #
  209. # number_to_rounded(111.2345) # => 111.235
  210. # number_to_rounded(111.2345, precision: 2) # => 111.23
  211. # number_to_rounded(13, precision: 5) # => 13.00000
  212. # number_to_rounded(389.32314, precision: 0) # => 389
  213. # number_to_rounded(111.2345, significant: true) # => 111
  214. # number_to_rounded(111.2345, precision: 1, significant: true) # => 100
  215. # number_to_rounded(13, precision: 5, significant: true) # => 13.000
  216. # number_to_rounded(111.234, locale: :fr) # => 111,234
  217. #
  218. # number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true)
  219. # # => 13
  220. #
  221. # number_to_rounded(389.32314, precision: 4, significant: true) # => 389.3
  222. # number_to_rounded(1111.2345, precision: 2, separator: ',', delimiter: '.')
  223. # # => 1.111,23
  224. def number_to_rounded(number, options = {})
  225. return number unless valid_float?(number)
  226. number = Float(number)
  227. options = options.symbolize_keys
  228. defaults = format_translations('precision', options[:locale])
  229. options = defaults.merge!(options)
  230. precision = options.delete :precision
  231. significant = options.delete :significant
  232. strip_insignificant_zeros = options.delete :strip_insignificant_zeros
  233. if significant && precision > 0
  234. if number == 0
  235. digits, rounded_number = 1, 0
  236. else
  237. digits = (Math.log10(number.abs) + 1).floor
  238. rounded_number = (BigDecimal.new(number.to_s) / BigDecimal.new((10 ** (digits - precision)).to_f.to_s)).round.to_f * 10 ** (digits - precision)
  239. digits = (Math.log10(rounded_number.abs) + 1).floor # After rounding, the number of digits may have changed
  240. end
  241. precision -= digits
  242. precision = 0 if precision < 0 # don't let it be negative
  243. else
  244. rounded_number = BigDecimal.new(number.to_s).round(precision).to_f
  245. rounded_number = rounded_number.abs if rounded_number.zero? # prevent showing negative zeros
  246. end
  247. formatted_number = self.number_to_delimited("%01.#{precision}f" % rounded_number, options)
  248. if strip_insignificant_zeros
  249. escaped_separator = Regexp.escape(options[:separator])
  250. formatted_number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
  251. else
  252. formatted_number
  253. end
  254. end
  255. # Formats the bytes in +number+ into a more understandable
  256. # representation (e.g., giving it 1500 yields 1.5 KB). This
  257. # method is useful for reporting file sizes to users. You can
  258. # customize the format in the +options+ hash.
  259. #
  260. # See <tt>number_to_human</tt> if you want to pretty-print a
  261. # generic number.
  262. #
  263. # ==== Options
  264. #
  265. # * <tt>:locale</tt> - Sets the locale to be used for formatting
  266. # (defaults to current locale).
  267. # * <tt>:precision</tt> - Sets the precision of the number
  268. # (defaults to 3).
  269. # * <tt>:significant</tt> - If +true+, precision will be the #
  270. # of significant_digits. If +false+, the # of fractional
  271. # digits (defaults to +true+)
  272. # * <tt>:separator</tt> - Sets the separator between the
  273. # fractional and integer digits (defaults to ".").
  274. # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
  275. # to "").
  276. # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
  277. # insignificant zeros after the decimal separator (defaults to
  278. # +true+)
  279. # * <tt>:prefix</tt> - If +:si+ formats the number using the SI
  280. # prefix (defaults to :binary)
  281. #
  282. # ==== Examples
  283. #
  284. # number_to_human_size(123) # => 123 Bytes
  285. # number_to_human_size(1234) # => 1.21 KB
  286. # number_to_human_size(12345) # => 12.1 KB
  287. # number_to_human_size(1234567) # => 1.18 MB
  288. # number_to_human_size(1234567890) # => 1.15 GB
  289. # number_to_human_size(1234567890123) # => 1.12 TB
  290. # number_to_human_size(1234567, precision: 2) # => 1.2 MB
  291. # number_to_human_size(483989, precision: 2) # => 470 KB
  292. # number_to_human_size(1234567, precision: 2, separator: ',') # => 1,2 MB
  293. #
  294. # Non-significant zeros after the fractional separator are stripped out by
  295. # default (set <tt>:strip_insignificant_zeros</tt> to +false+ to change that):
  296. #
  297. # number_to_human_size(1234567890123, precision: 5) # => "1.1229 TB"
  298. # number_to_human_size(524288000, precision: 5) # => "500 MB"
  299. def number_to_human_size(number, options = {})
  300. options = options.symbolize_keys
  301. return number unless valid_float?(number)
  302. number = Float(number)
  303. defaults = format_translations('human', options[:locale])
  304. options = defaults.merge!(options)
  305. #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
  306. options[:strip_insignificant_zeros] = true if not options.key?(:strip_insignificant_zeros)
  307. storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
  308. base = options[:prefix] == :si ? 1000 : 1024
  309. if number.to_i < base
  310. unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true)
  311. storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit)
  312. else
  313. max_exp = STORAGE_UNITS.size - 1
  314. exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
  315. exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
  316. number /= base ** exponent
  317. unit_key = STORAGE_UNITS[exponent]
  318. unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true)
  319. formatted_number = self.number_to_rounded(number, options)
  320. storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
  321. end
  322. end
  323. # Pretty prints (formats and approximates) a number in a way it
  324. # is more readable by humans (eg.: 1200000000 becomes "1.2
  325. # Billion"). This is useful for numbers that can get very large
  326. # (and too hard to read).
  327. #
  328. # See <tt>number_to_human_size</tt> if you want to print a file
  329. # size.
  330. #
  331. # You can also define you own unit-quantifier names if you want
  332. # to use other decimal units (eg.: 1500 becomes "1.5
  333. # kilometers", 0.150 becomes "150 milliliters", etc). You may
  334. # define a wide range of unit quantifiers, even fractional ones
  335. # (centi, deci, mili, etc).
  336. #
  337. # ==== Options
  338. #
  339. # * <tt>:locale</tt> - Sets the locale to be used for formatting
  340. # (defaults to current locale).
  341. # * <tt>:precision</tt> - Sets the precision of the number
  342. # (defaults to 3).
  343. # * <tt>:significant</tt> - If +true+, precision will be the #
  344. # of significant_digits. If +false+, the # of fractional
  345. # digits (defaults to +true+)
  346. # * <tt>:separator</tt> - Sets the separator between the
  347. # fractional and integer digits (defaults to ".").
  348. # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
  349. # to "").
  350. # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
  351. # insignificant zeros after the decimal separator (defaults to
  352. # +true+)
  353. # * <tt>:units</tt> - A Hash of unit quantifier names. Or a
  354. # string containing an i18n scope where to find this hash. It
  355. # might have the following keys:
  356. # * *integers*: <tt>:unit</tt>, <tt>:ten</tt>,
  357. # *<tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>,
  358. # *<tt>:billion</tt>, <tt>:trillion</tt>,
  359. # *<tt>:quadrillion</tt>
  360. # * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>,
  361. # *<tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>,
  362. # *<tt>:pico</tt>, <tt>:femto</tt>
  363. # * <tt>:format</tt> - Sets the format of the output string
  364. # (defaults to "%n %u"). The field types are:
  365. # * %u - The quantifier (ex.: 'thousand')
  366. # * %n - The number
  367. #
  368. # ==== Examples
  369. #
  370. # number_to_human(123) # => "123"
  371. # number_to_human(1234) # => "1.23 Thousand"
  372. # number_to_human(12345) # => "12.3 Thousand"
  373. # number_to_human(1234567) # => "1.23 Million"
  374. # number_to_human(1234567890) # => "1.23 Billion"
  375. # number_to_human(1234567890123) # => "1.23 Trillion"
  376. # number_to_human(1234567890123456) # => "1.23 Quadrillion"
  377. # number_to_human(1234567890123456789) # => "1230 Quadrillion"
  378. # number_to_human(489939, precision: 2) # => "490 Thousand"
  379. # number_to_human(489939, precision: 4) # => "489.9 Thousand"
  380. # number_to_human(1234567, precision: 4,
  381. # significant: false) # => "1.2346 Million"
  382. # number_to_human(1234567, precision: 1,
  383. # separator: ',',
  384. # significant: false) # => "1,2 Million"
  385. #
  386. # Non-significant zeros after the decimal separator are stripped
  387. # out by default (set <tt>:strip_insignificant_zeros</tt> to
  388. # +false+ to change that):
  389. #
  390. # number_to_human(12345012345, significant_digits: 6) # => "12.345 Billion"
  391. # number_to_human(500000000, precision: 5) # => "500 Million"
  392. #
  393. # ==== Custom Unit Quantifiers
  394. #
  395. # You can also use your own custom unit quantifiers:
  396. # number_to_human(500000, :units => {:unit => "ml", :thousand => "lt"}) # => "500 lt"
  397. #
  398. # If in your I18n locale you have:
  399. #
  400. # distance:
  401. # centi:
  402. # one: "centimeter"
  403. # other: "centimeters"
  404. # unit:
  405. # one: "meter"
  406. # other: "meters"
  407. # thousand:
  408. # one: "kilometer"
  409. # other: "kilometers"
  410. # billion: "gazillion-distance"
  411. #
  412. # Then you could do:
  413. #
  414. # number_to_human(543934, :units => :distance) # => "544 kilometers"
  415. # number_to_human(54393498, :units => :distance) # => "54400 kilometers"
  416. # number_to_human(54393498000, :units => :distance) # => "54.4 gazillion-distance"
  417. # number_to_human(343, :units => :distance, :precision => 1) # => "300 meters"
  418. # number_to_human(1, :units => :distance) # => "1 meter"
  419. # number_to_human(0.34, :units => :distance) # => "34 centimeters"
  420. def number_to_human(number, options = {})
  421. options = options.symbolize_keys
  422. return number unless valid_float?(number)
  423. number = Float(number)
  424. defaults = format_translations('human', options[:locale])
  425. options = defaults.merge!(options)
  426. #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
  427. options[:strip_insignificant_zeros] = true if not options.key?(:strip_insignificant_zeros)
  428. inverted_du = DECIMAL_UNITS.invert
  429. units = options.delete :units
  430. unit_exponents = case units
  431. when Hash
  432. units
  433. when String, Symbol
  434. I18n.translate(:"#{units}", :locale => options[:locale], :raise => true)
  435. when nil
  436. I18n.translate(:"number.human.decimal_units.units", :locale => options[:locale], :raise => true)
  437. else
  438. raise ArgumentError, ":units must be a Hash or String translation scope."
  439. end.keys.map{|e_name| inverted_du[e_name] }.sort_by{|e| -e}
  440. number_exponent = number != 0 ? Math.log10(number.abs).floor : 0
  441. display_exponent = unit_exponents.find{ |e| number_exponent >= e } || 0
  442. number /= 10 ** display_exponent
  443. unit = case units
  444. when Hash
  445. units[DECIMAL_UNITS[display_exponent]]
  446. when String, Symbol
  447. I18n.translate(:"#{units}.#{DECIMAL_UNITS[display_exponent]}", :locale => options[:locale], :count => number.to_i)
  448. else
  449. I18n.translate(:"number.human.decimal_units.units.#{DECIMAL_UNITS[display_exponent]}", :locale => options[:locale], :count => number.to_i)
  450. end
  451. decimal_format = options[:format] || I18n.translate(:'number.human.decimal_units.format', :locale => options[:locale], :default => "%n %u")
  452. formatted_number = self.number_to_rounded(number, options)
  453. decimal_format.gsub(/%n/, formatted_number).gsub(/%u/, unit).strip
  454. end
  455. def self.private_module_and_instance_method(method_name)
  456. private method_name
  457. private_class_method method_name
  458. end
  459. private_class_method :private_module_and_instance_method
  460. def format_translations(namespace, locale) #:nodoc:
  461. defaults_translations(locale).merge(translations_for(namespace, locale))
  462. end
  463. private_module_and_instance_method :format_translations
  464. def defaults_translations(locale) #:nodoc:
  465. I18n.translate(:'number.format', :locale => locale, :default => {})
  466. end
  467. private_module_and_instance_method :defaults_translations
  468. def translations_for(namespace, locale) #:nodoc:
  469. I18n.translate(:"number.#{namespace}.format", :locale => locale, :default => {})
  470. end
  471. private_module_and_instance_method :translations_for
  472. def valid_float?(number) #:nodoc:
  473. Float(number)
  474. rescue ArgumentError, TypeError
  475. false
  476. end
  477. private_module_and_instance_method :valid_float?
  478. end
  479. end