Class: Numeric
- Inherits:
-
Object
- Object
- Numeric
- Defined in:
- lib/merb-helpers/core_ext/numeric.rb
Defined Under Namespace
Modules: Transformer
Instance Method Summary collapse
-
#minutes_to_hours ⇒ Object
Converts a
numeric
value representing minutes into a string representing an hour value. -
#to_currency(format_name = nil, options = {}) ⇒ Object
Formats into a currency string (e.g., $13.65).
-
#two_digits ⇒ Object
Formats a
number
into a two digit string. -
#with_delimiter(format_name = nil, options = {}) ⇒ Object
Formats with with grouped thousands using
delimiter
(e.g., 12,324). -
#with_precision(format_name = nil, options = {}) ⇒ Object
Formats with a level of
:precision
(e.g., 112.32 has a precision of 2).
Instance Method Details
#minutes_to_hours ⇒ 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
315.minutes_to_hours => “05:15”
384 385 386 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 384 def minutes_to_hours Transformer.minutes_to_hours(self) end |
#to_currency(format_name = nil, options = {}) ⇒ Object
Formats into a currency string (e.g., $13.65). You can specify a format to use and even overwrite some of the format 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 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
1234567890.506.to_currency(:US) # => “$1,234,567,890.51” 1234567890.506.to_currency(:US, :precision => 1) # => “$1,234,567,890.5” 1234567890.516.to_currency(:FR) # =>“1 234 567 890,52€” 1234567890.516.to_currency(:US, :unit => “€”) # =>“€1,234,567,890.52” 1234567890.506.to_currency(:US, :precision => 3, :unit => “€”) # => “€1,234,567,890.506” 1234567890.506.to_currency(:AU, :unit => “$AUD”, :format => ‘%n %u’) # => “1,234,567,890.51 $AUD”
353 354 355 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 353 def to_currency(format_name = nil, = {}) Transformer.to_currency(self, format_name, ) end |
#two_digits ⇒ Object
Formats a number
into a two digit string. Basically it prepends an integer to a 2 digits string.
Returns
- String
-
a string representing the number converted into a 2 digits string.
Examples
(5-3).two_digits # => “02”
367 368 369 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 367 def two_digits Transformer.two_digits(self) end |
#with_delimiter(format_name = nil, options = {}) ⇒ Object
Formats with 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
12345678.with_delimiter # => 12,345,678 12345678.05.with_delimiter # => 12,345,678.05 12345678.with_delimiter(:FR) # => 12.345.678 12345678.with_delimiter(:US) # => 12,345,678
295 296 297 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 295 def with_delimiter(format_name = nil, = {}) Transformer.with_delimiter(self, format_name, ) end |
#with_precision(format_name = nil, options = {}) ⇒ Object
Formats 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
111.2345.with_precision # => 111.235 111.2345.with_precision(:UK, :precision => 1) # => “111.2” 1234.567.with_precision(:US, :precision => 1, :separator => ‘,’, :delimiter => ‘-’) # => “1-234,6”
322 323 324 |
# File 'lib/merb-helpers/core_ext/numeric.rb', line 322 def with_precision(format_name = nil, = {}) Transformer.with_precision(self, format_name, ) end |