Class: Plurimath::Formatter::Numbers::Integer
- Defined in:
- lib/plurimath/formatter/numbers/integer.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
Instance Method Summary collapse
- #apply(number, options = {}) ⇒ Object
- #chop_group(string, size) ⇒ Object
- #format_groups(string) ⇒ Object
-
#initialize(symbols = {}) ⇒ Integer
constructor
A new instance of Integer.
Methods inherited from Base
Constructor Details
#initialize(symbols = {}) ⇒ Integer
Returns a new instance of Integer.
9 10 11 12 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 9 def initialize(symbols = {}) @groups = Array(symbols[:group_digits] || 3) @separator = symbols[:group] || ',' end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 7 def format @format end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
7 8 9 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 7 def groups @groups end |
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
7 8 9 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 7 def separator @separator end |
Instance Method Details
#apply(number, options = {}) ⇒ Object
14 15 16 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 14 def apply(number, = {}) format_groups(interpolate(number, number.to_i)) end |
#chop_group(string, size) ⇒ Object
29 30 31 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 29 def chop_group(string, size) string.slice!([string.size - size, 0].max, size) end |
#format_groups(string) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 18 def format_groups(string) return string if groups.empty? tokens = [] tokens << chop_group(string, groups.first) tokens << chop_group(string, groups.last) until string.empty? tokens.compact.reverse.join(separator) end |