Class: Plurimath::Formatter::Numbers::Significant
- Inherits:
-
Object
- Object
- Plurimath::Formatter::Numbers::Significant
- Defined in:
- lib/plurimath/formatter/numbers/significant.rb
Instance Attribute Summary collapse
-
#decimal ⇒ Object
Returns the value of attribute decimal.
-
#significant ⇒ Object
Returns the value of attribute significant.
-
#symbols ⇒ Object
Returns the value of attribute symbols.
Instance Method Summary collapse
- #apply(string, int_format, frac_format) ⇒ Object
-
#initialize(symbols) ⇒ Significant
constructor
A new instance of Significant.
Constructor Details
#initialize(symbols) ⇒ Significant
Returns a new instance of Significant.
9 10 11 12 13 |
# File 'lib/plurimath/formatter/numbers/significant.rb', line 9 def initialize(symbols) @symbols = symbols @decimal = symbols[:decimal] @significant = symbols[:significant].to_i end |
Instance Attribute Details
#decimal ⇒ Object
Returns the value of attribute decimal.
7 8 9 |
# File 'lib/plurimath/formatter/numbers/significant.rb', line 7 def decimal @decimal end |
#significant ⇒ Object
Returns the value of attribute significant.
7 8 9 |
# File 'lib/plurimath/formatter/numbers/significant.rb', line 7 def significant @significant end |
#symbols ⇒ Object
Returns the value of attribute symbols.
7 8 9 |
# File 'lib/plurimath/formatter/numbers/significant.rb', line 7 def symbols @symbols end |
Instance Method Details
#apply(string, int_format, frac_format) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/plurimath/formatter/numbers/significant.rb', line 15 def apply(string, int_format, frac_format) return string if significant.zero? return string unless string.match?(/[1-9]/) chars = string.split("") return string if count_chars(chars, true) == significant string = signify(chars) integer, fraction = string.split(decimal) string = [format_groups(int_format, integer)] string << format_groups(frac_format, fraction) if fraction string.join(decimal) end |