PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/actionpack/lib/action_view/helpers/number_helper.rb

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