Class: Plurimath::Formatter::Numbers::Significant

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/formatter/numbers/significant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#decimalObject

Returns the value of attribute decimal.



7
8
9
# File 'lib/plurimath/formatter/numbers/significant.rb', line 7

def decimal
  @decimal
end

#significantObject

Returns the value of attribute significant.



7
8
9
# File 'lib/plurimath/formatter/numbers/significant.rb', line 7

def significant
  @significant
end

#symbolsObject

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