Module: Numeric::Transformer
- Defined in:
- lib/merb-helpers/core_ext/numeric.rb
Class Method Summary collapse
-
.add_format(format) ⇒ Object
Adds a new format to the existing transforming formats.
-
.change_default_format(format_code) ⇒ Object
Changes the default format to use when transforming a
Numeric
instance. -
.default_format ⇒ Object
Accessor for the default format in use.
-
.formats ⇒ Object
accessor for @formats —.
-
.minutes_to_hours(minutes) ⇒ Object
Converts a
numeric
value representing minutes into a string representing an hour value. -
.to_currency(number, format_name = nil, options = {}) ⇒ Object
Formats a
number
into a currency string (e.g., $13.65). -
.two_digits(number) ⇒ Object
Formats a
number
into a two digit string. -
.with_delimiter(number, format_name = nil, options = {}) ⇒ Object
Formats a
number
with grouped thousands usingdelimiter
(e.g., 12,324). -
.with_precision(number, format_name = nil, options = {}) ⇒ Object
Formats a
number
with a level of:precision
(e.g., 112.32 has a precision of 2).
Class Method Details
.add_format(format) ⇒ Object
Adds a new format to the existing transforming formats
Parameters
- format <Hash>
-
format defining how to transform numeric values
Examples
115 116 117 118 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 115 def self.add_format(format) formats.merge!(format) formats[format] end |
.change_default_format(format_code) ⇒ Object
Changes the default format to use when transforming a Numeric
instance
Parameters
- format_code <Symbol>
-
format name to use as the new default format
Returns
- Hash
-
a hash representing the default format
100 101 102 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 100 def self.change_default_format(format_code) @default_format = (formats[format_code] || default_format) end |
.default_format ⇒ Object
Accessor for the default format in use
85 86 87 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 85 def self.default_format @default_format end |
.formats ⇒ Object
accessor for @formats
74 75 76 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 74 def self.formats @formats end |
.minutes_to_hours(minutes) ⇒ Object
Converts a numeric
value representing minutes into a string representing an hour value
Parameters
- number<Numeric>
-
Numeric value representing minutes to convert in hours
Returns
- String
-
a string representing the numeric value converted in hours
Examples
minutes_to_hours(315) => “05:15”
264 265 266 267 268 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 264 def self.minutes_to_hours(minutes) hours = (minutes/60).ceil minutes = (minutes - (hours * 60)).to_i "#{two_digits(hours)}:#{two_digits(minutes)}" end |
.to_currency(number, format_name = nil, options = {}) ⇒ Object
Formats a number
into a currency string (e.g., $13.65). You can specify a format to use and even overwrite some of the format options.
Parameters
- number<Numeric>
-
Numeric value to convert
- format_name<Symbol>
-
name of the format to use
- options<Hash>
-
options which will overwrite the used format
Returns
- String
-
a string representing the number converted in currency
Options
:precision - Sets the level of precision :unit - Sets the denomination of the currency :format - Sets the format of the output string (defaults to “%u%n”). The field types are:
%u The currency unit %n The number
Examples
to_currency(1234567890.506, :US, :precision => 1) # => “$1,234,567,890.5” to_currency(1234567890.516, :FR) # =>“1 234 567 890,52€” to_currency(1234567890.516, :US, :unit => “€”) # =>“€1,234,567,890.52” to_currency(1234567890.506, :US, :precision => 3, :unit => “€”) # => “€1,234,567,890.506” to_currency(1234567890.506, :AU, :unit => “$AUD”, :format => ‘%n %u’) # => “1,234,567,890.51 $AUD”
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 221 def self.to_currency(number, format_name = nil, = {}) format = (formats[format_name] || default_format)[:currency].merge() begin format[:format].gsub(/%n/, with_precision(number, format_name, :precision => format[:precision]) ).gsub(/%u/, format[:unit]) rescue number end end |
.two_digits(number) ⇒ Object
Formats a number
into a two digit string. Basically it prepends an integer to a 2 digits string.
Parameters
- number<Numeric>
-
Numeric value to convert
Returns
- String
-
a string representing the number converted into a 2 digits string.
Examples
two_digits(5-3) # => “02”
247 248 249 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 247 def self.two_digits(number) (0..9).include?(number) ? "0#{number}" : number.to_s end |
.with_delimiter(number, format_name = nil, options = {}) ⇒ Object
Formats a number
with grouped thousands using delimiter
(e.g., 12,324). You can pass another format to format the number differently.
Parameters
- format_name<Symbol>
-
name of the format to use
- options<Hash>
-
options which will overwrite the used format
Returns
- String
-
a string representing the delimited number
Options
:delimiter - Overwrites the thousands delimiter. :separator - Overwrites the separator between the units.
Examples
with_delimiter(12345678) # => 12,345,678 with_delimiter(12345678.05) # => 12,345,678.05 with_delimiter(12345678, :FR) # => 12.345.678 with_delimiter(12345678, :US) # => 12,345,678
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 144 def self.with_delimiter(number, format_name = nil, = {}) format = (formats[format_name] || default_format)[:number].merge() begin parts = number.to_s.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{format[:delimiter]}") parts.join(format[:separator]) rescue number end end |
.with_precision(number, format_name = nil, options = {}) ⇒ Object
Formats a number
with a level of :precision
(e.g., 112.32 has a precision of 2). You can pass another format to use and even overwrite the format’s options.
Parameters
- format_name<Symbol>
-
name of the format to use
- options<Hash>
-
options which will overwrite the used format
Returns
- String
-
a string representing the delimited number
Options
:precision - Overwrites the level of precision :separator - Overwrites the separator between the units :delimiter - Overwrites the thousands delimiter
Examples
with_precision(111.2345) # => 111.235 with_precision(111.2345, :UK, :precision => 1) # => “111.2” with_precision(1234.567, :US, :precision => 1, :separator => ‘,’, :delimiter => ‘-’) # => “1-234,6”
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 180 def self.with_precision(number, format_name = nil, ={}) format = (formats[format_name] || default_format)[:number].merge() begin rounded_number = (Float(number) * (10 ** format[:precision])).round.to_f / 10 ** format[:precision] with_delimiter("%01.#{format[:precision]}f" % rounded_number, format_name, :delimiter => format[:delimiter], :separator => format[:separator]) rescue number end end |