Class: Plurimath::Formatter::Numbers::FormattedNumber

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_notationObject (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_separatorObject (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_partObject (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_partObject (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_signObject (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

#signObject (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

Returns:

  • (Boolean)


38
39
40
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 38

def base_notation?
  !base_notation.default?
end

#digits_stringObject

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 34

def fractional?
  !fraction_part.empty?
end

#negative?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 30

def negative?
  sign == -1
end

#sign_textObject

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_sObject



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_strObject



59
60
61
# File 'lib/plurimath/formatter/numbers/formatted_number.rb', line 59

def to_str
  to_s
end