PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/ruby/1.8/gems/actionpack-3.0.3/lib/action_view/helpers/number_helper.rb

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