Class: Plurimath::Formatter::Numbers::FormattedNumber
- Inherits:
-
Object
- Object
- Plurimath::Formatter::Numbers::FormattedNumber
- Defined in:
- lib/plurimath/formatter/numbers/formatted_number.rb
Overview
Structured result of number formatting. Carries sign, digit parts, decimal separator, and base notation as separate semantic elements so output renderers (MathML, LaTeX, etc.) can produce structured representations instead of flat strings.
Instance Attribute Summary collapse
-
#base_notation ⇒ Object
readonly
Returns the value of attribute base_notation.
-
#decimal_separator ⇒ Object
readonly
Returns the value of attribute decimal_separator.
-
#fraction_part ⇒ Object
readonly
Returns the value of attribute fraction_part.
-
#integer_part ⇒ Object
readonly
Returns the value of attribute integer_part.
-
#number_sign ⇒ Object
readonly
Returns the value of attribute number_sign.
-
#sign ⇒ Object
readonly
Returns the value of attribute sign.
Instance Method Summary collapse
- #base_notation? ⇒ Boolean
-
#digits_string ⇒ Object
Digits with decimal separator and optional hex capitalization, but without sign, prefix, or postfix.
- #fractional? ⇒ Boolean
-
#initialize(sign:, integer_part:, fraction_part:, decimal_separator:, base_notation:, number_sign: nil) ⇒ FormattedNumber
constructor
A new instance of FormattedNumber.
- #negative? ⇒ Boolean
-
#sign_text ⇒ Object
The sign as a rendering prefix: “-” for negative, “+” when number_sign is :plus, nil otherwise.
- #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(sign:, integer_part:, fraction_part:, decimal_separator:, base_notation:, number_sign: nil) ⇒ FormattedNumber
Returns a new instance of FormattedNumber.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 14 def initialize( sign:, integer_part:, fraction_part:, decimal_separator:, base_notation:, number_sign: nil ) @sign = sign @integer_part = integer_part @fraction_part = fraction_part @decimal_separator = decimal_separator @base_notation = base_notation @number_sign = number_sign end |
Instance Attribute Details
#base_notation ⇒ Object (readonly)
Returns the value of attribute base_notation.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 11 def base_notation @base_notation end |
#decimal_separator ⇒ Object (readonly)
Returns the value of attribute decimal_separator.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 11 def decimal_separator @decimal_separator end |
#fraction_part ⇒ Object (readonly)
Returns the value of attribute fraction_part.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 11 def fraction_part @fraction_part end |
#integer_part ⇒ Object (readonly)
Returns the value of attribute integer_part.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 11 def integer_part @integer_part end |
#number_sign ⇒ Object (readonly)
Returns the value of attribute number_sign.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 11 def number_sign @number_sign end |
#sign ⇒ Object (readonly)
Returns the value of attribute sign.
11 12 13 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 11 def sign @sign end |
Instance Method Details
#base_notation? ⇒ Boolean
38 39 40 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 38 def base_notation? !base_notation.default? end |
#digits_string ⇒ Object
Digits with decimal separator and optional hex capitalization, but without sign, prefix, or postfix. Used by structured renderers that handle sign and base notation as separate elements.
66 67 68 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 66 def digits_string formatted_digits end |
#fractional? ⇒ Boolean
34 35 36 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 34 def fractional? !fraction_part.empty? end |
#negative? ⇒ Boolean
30 31 32 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 30 def negative? sign == -1 end |
#sign_text ⇒ Object
The sign as a rendering prefix: “-” for negative, “+” when number_sign is :plus, nil otherwise. Output renderers use this to produce format-specific sign elements.
45 46 47 48 49 50 51 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 45 def sign_text if negative? "-" elsif number_sign == :plus "+" end end |
#to_s ⇒ Object
53 54 55 56 57 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 53 def to_s digits = formatted_digits digits = base_notation.wrap(digits) unless base_notation.default? "#{sign_text}#{digits}" end |
#to_str ⇒ Object
59 60 61 |
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 59 def to_str to_s end |