Class: Numerals::Format::Mode

Inherits:
Numerals::FormattingAspect show all
Includes:
ModalSupport::StateEquivalent
Defined in:
lib/numerals/format/mode.rb

Overview

Formatting mode

  • :scientific use scientific notation.

  • :fixed used fixed notation.

  • :general (the default) uses :fixed notation unless it would produce trailing zeros in the integer part or too many leading zeros in the fractional part. The intent is to produced the simplest or more natural output and it’s regulated by the :max_leading and

:max_trailing parameters.

The special value :engineering can be used as a shortcut for :scientific mode with :engineering :sci_int_digits.

The modes can be abbreviated as :sci, :fix, :gen and :end.

  • :sci_int_digits numbe of digits to the left of the point in scientific notation. By default 1. The special value :engineering can be used for engineering notation, i.e. the number of digits will be between one and 3 so that the exponent is a multiple of 3. The special value :all is used to make the significand an integer value.

  • :max_leading (maximum number of leading zeros) determines when :scientific is chosen instead of :fixed when the mode is :general. The default value, 5 is that of the General Decimal Arithmetic ‘to-scientific-string’ number to text conversion, and also used by the .NET General (“G”) format specifier. To reproduce the behaviour of the C %g format specifier the value should be 3. The special value :all can be used to reproduce the behaviour of some calculators, where scientific notation is used when more digits than the specified precision would be needed.

Constant Summary collapse

DEFAULTS =
{
  mode: :general,
  sci_int_digits: 1,
  max_leading: 5,
  max_trailing: 0,
  base_scale: 1
}
MODE_SHORTCUTS =
{
  gen: :general,
  sci: :scientific,
  fix: :fixed,
  eng: :engineering
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Numerals::FormattingAspect

#[], [], aspect, #set, set

Constructor Details

#initialize(*args) ⇒ Mode

Returns a new instance of Mode.



48
49
50
51
52
53
# File 'lib/numerals/format/mode.rb', line 48

def initialize(*args)
  DEFAULTS.each do |param, value|
    instance_variable_set "@#{param}", value
  end
  set! *args
end

Instance Attribute Details

#base_scaleObject

Returns the value of attribute base_scale.



55
56
57
# File 'lib/numerals/format/mode.rb', line 55

def base_scale
  @base_scale
end

#max_leadingObject

Returns the value of attribute max_leading.



55
56
57
# File 'lib/numerals/format/mode.rb', line 55

def max_leading
  @max_leading
end

#max_trailingObject

Returns the value of attribute max_trailing.



55
56
57
# File 'lib/numerals/format/mode.rb', line 55

def max_trailing
  @max_trailing
end

#modeObject

Returns the value of attribute mode.



55
56
57
# File 'lib/numerals/format/mode.rb', line 55

def mode
  @mode
end

#sci_int_digitsObject

Returns the value of attribute sci_int_digits.



55
56
57
# File 'lib/numerals/format/mode.rb', line 55

def sci_int_digits
  @sci_int_digits
end

Instance Method Details

#engineering?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/numerals/format/mode.rb', line 90

def engineering?
  @mode == :scientific && @sci_int_digits == :engineering
end

#fixed?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/numerals/format/mode.rb', line 98

def fixed?
  @mode == :fixed
end

#general?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/numerals/format/mode.rb', line 102

def general?
  @mode == :general
end

#inspectObject



125
126
127
# File 'lib/numerals/format/mode.rb', line 125

def inspect
  "Format::#{self}"
end

#parameters(abbreviated = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/numerals/format/mode.rb', line 106

def parameters(abbreviated=false)
  params = {}
  DEFAULTS.each do |param, default|
    value = instance_variable_get("@#{param}")
    if !abbreviated || value != default
      params[param] = value
    end
  end
  if abbreviated && engineering?
    params[:mode] = :engineering
    params.delete :sci_int_digits
  end
  params
end

#scientific?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/numerals/format/mode.rb', line 94

def scientific?
  @mode == :scientific
end

#to_sObject



121
122
123
# File 'lib/numerals/format/mode.rb', line 121

def to_s
  "Mode[#{parameters(true).inspect.unwrap('{}')}]"
end