Class: Plurimath::Formatter::Numbers::Fraction
- Defined in:
- lib/plurimath/formatter/numbers/fraction.rb
Overview
Transforms fraction digits on Parts before localized rendering.
Constant Summary collapse
- DEFAULT_PRECISION =
FormatOptions::DEFAULT_FRACTION_PRECISION
- DEFAULT_STRINGS =
{ empty: "", zero: "0", dot: "." }.freeze
Constants inherited from Base
Base::DEFAULT_BASE, Base::DIGIT_VALUE, Base::HEX_ALPHANUMERIC, Base::HEX_DIGITS
Instance Attribute Summary collapse
-
#decimal ⇒ Object
readonly
Returns the value of attribute decimal.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
Attributes inherited from Base
Instance Method Summary collapse
-
#apply_parts(parts, precision: self.precision) ⇒ Object
Keep fraction preparation on structured parts; localized rendering and grouping happen later at the PartsRenderer boundary.
- #format(number, precision) ⇒ Object
- #format_groups(string, length = group) ⇒ Object
-
#initialize(options) ⇒ Fraction
constructor
A new instance of Fraction.
Constructor Details
#initialize(options) ⇒ Fraction
Returns a new instance of Fraction.
13 14 15 16 17 18 19 20 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 13 def initialize() super @group = self..fraction_group_digits @decimal = self..decimal @separator = self..fraction_group @precision = self..precision || DEFAULT_PRECISION @digit_count = self..digit_count end |
Instance Attribute Details
#decimal ⇒ Object (readonly)
Returns the value of attribute decimal.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8 def decimal @decimal end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8 def group @group end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8 def precision @precision end |
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
8 9 10 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8 def separator @separator end |
Instance Method Details
#apply_parts(parts, precision: self.precision) ⇒ Object
Keep fraction preparation on structured parts; localized rendering and grouping happen later at the PartsRenderer boundary.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 24 def apply_parts(parts, precision: self.precision) precision = precision.to_i return parts.with_digits(fraction_digits: DEFAULT_STRINGS[:empty]) unless precision.positive? @integer_digits = parts.integer_digits fraction = parts.fraction_digits fraction = change_base(fraction, precision) if !base_default? && fraction.match?(/[1-9]/) number = if @digit_count.positive? digit_count_format(fraction, precision) else format(fraction, precision) end parts.with_digits( integer_digits: integer_digits, fraction_digits: number.to_s, ) end |
#format(number, precision) ⇒ Object
44 45 46 47 48 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 44 def format(number, precision) return number if precision <= number.length number + (DEFAULT_STRINGS[:zero] * (precision - number.length)) end |
#format_groups(string, length = group) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 50 def format_groups(string, length = group) string = capitalize_hex_digits(string) length = string.length if group.to_i.zero? change_format(string, length) end |