0
@@ -28,5 +28,74 @@ module Merb
0
"<form action=\"#{url}\" method=\"post\" class=\"destroy\"><button type=\"submit\" class=\"destroy\">#{text}</button></form>"
0
+ # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You
0
+ # can customize the format using optional <em>delimiter</em> and <em>separator</em> parameters.
0
+ # * <tt>delimiter</tt> - Sets the thousands delimiter (defaults to ",").
0
+ # * <tt>separator</tt> - Sets the separator between the units (defaults to ".").
0
+ # number_with_delimiter(12345678) # => 12,345,678
0
+ # number_with_delimiter(12345678.05) # => 12,345,678.05
0
+ # number_with_delimiter(12345678, ".") # => 12.345.678
0
+ # number_with_delimiter(98765432.98, " ", ",")
0
+ def number_with_delimiter(number, delimiter=",", separator=".")
0
+ parts = number.to_s.split('.')
0
+ parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
0
+ # Formats a +number+ as a percentage string (e.g., 65%). You can customize the
0
+ # format in the +options+ hash.
0
+ # * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
0
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
0
+ # number_to_percentage(100) # => 100.000%
0
+ # number_to_percentage(100, :precision => 0) # => 100%
0
+ # number_to_percentage(302.24398923423, :precision => 5)
0
+ def number_to_percentage(number, options = {})
0
+ options = options.stringify_keys
0
+ precision = options["precision"] || 3
0
+ separator = options["separator"] || "."
0
+ number = number_with_precision(number, precision)
0
+ parts = number.split('.')
0
+ parts[0] + separator + parts[1].to_s + "%"
0
+ # Formats a +number+ with the specified level of +precision+ (e.g., 112.32 has a precision of 2). The default
0
+ # level of precision is 3.
0
+ # number_with_precision(111.2345) # => 111.235
0
+ # number_with_precision(111.2345, 2) # => 111.23
0
+ # number_with_precision(13, 5) # => 13.00000
0
+ # number_with_precision(389.32314, 0) # => 389
0
+ def number_with_precision(number, precision=3)
0
+ "%01.#{precision}f" % ((Float(number) * (10 ** precision)).round.to_f / 10 ** precision)
Comments
No one has commented yet.