Class: Plurimath::Formatter::Numbers::Integer
- Defined in:
- lib/plurimath/formatter/numbers/integer.rb
Overview
Converts integer digits to the target base and applies integer grouping.
Constant Summary
Constants inherited from Base
Base::DEFAULT_BASE, Base::DIGIT_VALUE, Base::HEX_ALPHANUMERIC, Base::HEX_DIGITS
Instance Attribute Summary collapse
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#padding_digits ⇒ Object
readonly
Returns the value of attribute padding_digits.
-
#padding_group_digits ⇒ Object
readonly
Returns the value of attribute padding_group_digits.
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
Attributes inherited from Base
Instance Method Summary collapse
- #apply(number) ⇒ Object
- #chop_group(string, size) ⇒ Object
- #format_groups(string) ⇒ Object
-
#initialize(options) ⇒ Integer
constructor
A new instance of Integer.
- #number_to_base(number) ⇒ Object
Constructor Details
#initialize(options) ⇒ Integer
Returns a new instance of Integer.
11 12 13 14 15 16 17 18 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 11 def initialize() super @groups = self..group_digits @separator = self..group @padding = self..padding @padding_digits = self..padding_digits @padding_group_digits = self..padding_group_digits end |
Instance Attribute Details
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 8 def groups @groups end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 8 def padding @padding end |
#padding_digits ⇒ Object (readonly)
Returns the value of attribute padding_digits.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 8 def padding_digits @padding_digits end |
#padding_group_digits ⇒ Object (readonly)
Returns the value of attribute padding_group_digits.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 8 def padding_group_digits @padding_group_digits end |
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 8 def separator @separator end |
Instance Method Details
#apply(number) ⇒ Object
20 21 22 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 20 def apply(number) format_groups(number_to_base(number)) end |
#chop_group(string, size) ⇒ Object
37 38 39 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 37 def chop_group(string, size) string.slice([string.size - size, 0].max, size) end |
#format_groups(string) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 24 def format_groups(string) string = capitalize_hex_digits(string) string = pad_integer(string) tokens = [] until string.empty? tokens << chop_group(string, groups) string = string[0...-tokens.last.size] end tokens.compact.reverse.join(separator) end |
#number_to_base(number) ⇒ Object
41 42 43 44 45 |
# File 'lib/plurimath/formatter/numbers/integer.rb', line 41 def number_to_base(number) return number if base_default? number.to_i.to_s(base) end |