Class: Numerals::Format
- Inherits:
-
FormattingAspect
- Object
- FormattingAspect
- Numerals::Format
- Defined in:
- lib/numerals/format/format.rb,
lib/numerals/format.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
-
#exact_input ⇒ Object
readonly
Returns the value of attribute exact_input.
-
#input_rounding ⇒ Object
readonly
Returns the value of attribute input_rounding.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#notation ⇒ Object
readonly
Returns the value of attribute notation.
-
#rounding ⇒ Object
readonly
Returns the value of attribute rounding.
-
#symbols ⇒ Object
readonly
Returns the value of attribute symbols.
Class Method Summary collapse
- .assemble(id, output, format, text_parts) ⇒ Object
- .define_notation(id, notation_class) ⇒ Object
- .disassemble(id, format, text) ⇒ Object
- .notation(id, format) ⇒ Object
Instance Method Summary collapse
- #base ⇒ Object
- #case_sensitive? ⇒ Boolean
- #dup ⇒ Object
-
#initialize(*args) ⇒ Format
constructor
A new instance of Format.
- #input_rounding? ⇒ Boolean
- #input_rounding_mode ⇒ Object
- #inspect ⇒ Object
- #parameters ⇒ Object
-
#significand_base ⇒ Object
Presentation base for the significand.
- #to_s ⇒ Object
Methods included from Input
#conversion_in, #partition_in, #read
Methods included from Output
Methods inherited from FormattingAspect
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_input ⇒ Object (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_rounding ⇒ Object (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 |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
56 57 58 |
# File 'lib/numerals/format/format.rb', line 56 def mode @mode end |
#notation ⇒ Object (readonly)
Returns the value of attribute notation.
56 57 58 |
# File 'lib/numerals/format/format.rb', line 56 def notation @notation end |
#rounding ⇒ Object (readonly)
Returns the value of attribute rounding.
56 57 58 |
# File 'lib/numerals/format/format.rb', line 56 def rounding @rounding end |
#symbols ⇒ Object (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
.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
#base ⇒ Object
59 60 61 |
# File 'lib/numerals/format/format.rb', line 59 def base @rounding.base end |
#case_sensitive? ⇒ Boolean
125 126 127 |
# File 'lib/numerals/format/format.rb', line 125 def case_sensitive? @symbols.case_sensitive? end |
#dup ⇒ Object
169 170 171 172 |
# File 'lib/numerals/format/format.rb', line 169 def dup # we need deep copy Format[parameters] end |
#input_rounding? ⇒ Boolean
70 71 72 |
# File 'lib/numerals/format/format.rb', line 70 def input_rounding? !@input_rounding.nil? end |
#input_rounding_mode ⇒ Object
74 75 76 |
# File 'lib/numerals/format/format.rb', line 74 def input_rounding_mode input_rounding? ? @input_rounding.mode : nil end |
#inspect ⇒ Object
121 122 123 |
# File 'lib/numerals/format/format.rb', line 121 def inspect to_s end |
#parameters ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/numerals/format/format.rb', line 99 def parameters { rounding: @rounding, exact_input: @exact_input, mode: @mode, symbols: @symbols, notation: @notation, input_rounding: input_rounding? ? @input_rounding : nil } end |
#significand_base ⇒ Object
Presentation base for the significand
64 65 66 |
# File 'lib/numerals/format/format.rb', line 64 def significand_base base**@mode.base_scale end |
#to_s ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/numerals/format/format.rb', line 110 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 |