Class: DrLight::ScientificNumber::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/dr_light/scientific_number/formatter.rb

Overview

Class responsible for formatting the values of DrLight::ScientificNumber

Author:

  • darthjee

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, deviance:) ⇒ Formatter

Returns a new instance of Formatter.

Parameters:

  • value (Nuber)

    number to be exibed

  • deviance (Number)

    deviance of number



16
17
18
19
20
21
22
# File 'lib/dr_light/scientific_number/formatter.rb', line 16

def initialize(value:, deviance:)
  @value = value
  @deviance = deviance
  @significant = 1
  @exponential = 0
  format_value
end

Instance Attribute Details

#devianceObject (readonly)



11
12
13
# File 'lib/dr_light/scientific_number/formatter.rb', line 11

def deviance
  @deviance
end

#exponentialObject (readonly)



11
12
13
# File 'lib/dr_light/scientific_number/formatter.rb', line 11

def exponential
  @exponential
end

#significantObject (readonly)



11
12
13
# File 'lib/dr_light/scientific_number/formatter.rb', line 11

def significant
  @significant
end

#valueObject (readonly)



11
12
13
# File 'lib/dr_light/scientific_number/formatter.rb', line 11

def value
  @value
end

Instance Method Details

#format_stringString

Returns the string of the format expected for DrLight::ScientificNumber#to_s

Returns:

  • (String)


28
29
30
31
32
33
# File 'lib/dr_light/scientific_number/formatter.rb', line 28

def format_string
  ["%<value>.#{significant}f"].tap do |values|
    values << '(%<deviance>d)' unless deviance.zero?
    values << 'e%<exponential>d' unless exponential.zero?
  end.join
end