Class: Numerals::Format

Inherits:
FormattingAspect show all
Includes:
ModalSupport::StateEquivalent, Input, Output
Defined in:
lib/numerals/format/format.rb,
lib/numerals/format.rb,
lib/numerals/format/sugar.rb,
lib/numerals/format/notation.rb,
lib/numerals/format/notations/html.rb,
lib/numerals/format/notations/text.rb,
lib/numerals/format/notations/latex.rb

Overview

A Format object holds formatting options and performs formatted input/output operations on numbers.

Formatting options are grouped into aspects:

  • Exact input

  • Rounding

  • Mode

  • Symbols

  • Input rounding

Some aspects (Rounding, Mode & Symbols) are handled with aspect-definining classes Rounding, Format::Mode and Format::Symbols.

Exact input applies only to numeric types that can hold limited precision values such as Float, Flt::Num or BigDecimal. It specifies that the numeric value is to be taken as an exact quantity. Otherwise, the numeric value is interpreted as a rounded approximation of some original exact value (so it represents a range of exact number which would all be rounded to the same approximation). Rational and Integer types are always exact and not affected by this option.

Rounding defines how numbers are rounded into text form or how values represented in text round to numeric values. So it specifies the precision of the result and whether the result is an approximation or an exact quantity.

Mode defines de formatting style.

Symbols contains the details of how digits and other symbols are represented in text form and the final text notation used.

The input-rounding property can set to either a Rounding object or just a rounding mode symbol (:half_even, etc.). It is used to define which rounding mode is implied when reading textual numeric expressions into approximate numeric values. It affects how approximate numbers are written to text because the text representation of approximate values should be read back into the original value. If a Rounding object is assigned only the mode is used, and it is ignored if the rounding is exact. #

Defined Under Namespace

Modules: Input, Output Classes: BaseScaler, Error, ExpSetter, HtmlNotation, InvalidNumberFormat, InvalidNumericType, InvalidRepeatingNumeral, LatexNotation, Mode, Notation, Symbols, TextNotation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Input

#conversion_in, #partition_in, #read

Methods included from Output

#write

Methods inherited from FormattingAspect

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

Constructor Details

#initialize(*args) ⇒ Format

Returns a new instance of Format.



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

def initialize(*args)
  @exact_input = false
  @rounding = Rounding[:short]
  @mode = Mode[]
  @symbols = Symbols[]
  @notation = :text
  @input_rounding = nil
  set! *args
end

Instance Attribute Details

#exact_inputObject (readonly)

Returns the value of attribute exact_input.



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

def exact_input
  @exact_input
end

#input_roundingObject (readonly)

Returns the value of attribute input_rounding.



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

def input_rounding
  @input_rounding
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#notationObject (readonly)

Returns the value of attribute notation.



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

def notation
  @notation
end

#roundingObject (readonly)

Returns the value of attribute rounding.



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

def rounding
  @rounding
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



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

def symbols
  @symbols
end

Class Method Details

.<<(*args) ⇒ Object



4
5
6
# File 'lib/numerals/format/sugar.rb', line 4

def self.<<(*args)
  Format[].<<(*args)
end

.>>(*args) ⇒ Object



8
9
10
# File 'lib/numerals/format/sugar.rb', line 8

def self.>>(*args)
  Format[].>>(*args)
end

.assemble(id, output, format, text_parts) ⇒ Object



37
38
39
# File 'lib/numerals/format/notation.rb', line 37

def self.assemble(id, output, format, text_parts)
  notation(id, format).assemble(output, text_parts)
end

.define_notation(id, notation_class) ⇒ Object



25
26
27
28
29
30
# File 'lib/numerals/format/notation.rb', line 25

def self.define_notation(id, notation_class)
  unless notation_class.class == Class && notation_class.superclass == Notation
    raise "Notation class must be derived from Format::Notation"
  end
  @notations[id] = notation_class
end

.disassemble(id, format, text) ⇒ Object



41
42
43
# File 'lib/numerals/format/notation.rb', line 41

def self.disassemble(id, format, text)
  notation(id, format).disassemble(text)
end

.notation(id, format) ⇒ Object



32
33
34
# File 'lib/numerals/format/notation.rb', line 32

def self.notation(id, format)
  @notations[id].new(format) || raise("Unknown notation #{id.inspect}")
end

Instance Method Details

#<<(*args) ⇒ Object



12
13
14
# File 'lib/numerals/format/sugar.rb', line 12

def <<(*args)
  FormattingStream[self].<<(*args)
end

#>>(*args) ⇒ Object



16
17
18
# File 'lib/numerals/format/sugar.rb', line 16

def >>(*args)
  FormattingStream[self].>>(*args)
end

#baseObject



59
60
61
# File 'lib/numerals/format/format.rb', line 59

def base
  @rounding.base
end

#case_sensitive?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/numerals/format/format.rb', line 134

def case_sensitive?
  @symbols.case_sensitive?
end

#dupObject



182
183
184
185
# File 'lib/numerals/format/format.rb', line 182

def dup
  # we need deep copy
  Format[parameters]
end

#input_rounding?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/numerals/format/format.rb', line 78

def input_rounding?
  !@input_rounding.nil?
end

#input_rounding_modeObject



82
83
84
# File 'lib/numerals/format/format.rb', line 82

def input_rounding_mode
  input_rounding? ? @input_rounding.mode : nil
end

#inspectObject



130
131
132
# File 'lib/numerals/format/format.rb', line 130

def inspect
  to_s
end

#padded?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/numerals/format/format.rb', line 67

def padded?
  padding.padded?
end

#paddingObject



63
64
65
# File 'lib/numerals/format/format.rb', line 63

def padding
  @symbols.padding
end

#parametersObject



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

def parameters
  {
    rounding: @rounding,
    exact_input: @exact_input,
    mode: @mode,
    symbols: @symbols,
    notation: @notation,
    input_rounding: input_rounding? ? @input_rounding : nil
  }
end

#significand_baseObject

Presentation base for the significand



72
73
74
# File 'lib/numerals/format/format.rb', line 72

def significand_base
  base**@mode.base_scale
end

#to_sObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/numerals/format/format.rb', line 119

def to_s
  args = []
  args << "exact_input: true" if @exact_input
  args << "rounding: #{@rounding}"
  args << "mode: #{@mode}"
  args << "symbols: #{@symbols}"
  args << "notation: #{@notation.inspect}" if @notation != :text
  args << "input_rounding: #{input_rounding_mode.inspect}" if input_rounding?
  "Format[#{args.join(', ')}]"
end