Module: YaKansuji::Formatter
- Defined in:
- lib/ya_kansuji/formatter.rb,
lib/ya_kansuji/formatter/gov.rb,
lib/ya_kansuji/formatter/lawyer.rb,
lib/ya_kansuji/formatter/simple.rb,
lib/ya_kansuji/formatter/judic_h.rb,
lib/ya_kansuji/formatter/judic_v.rb
Overview
Shared helpers for built-in formatters.
Defined Under Namespace
Modules: Gov, JudicH, JudicV, Lawyer, Simple
Constant Summary collapse
- UNIT4_BASE =
10_000- UNIT4_CHUNK_LIMIT =
UNIT_EXP4.size + 1
- UNIT4_UNITS =
([''] + UNIT_EXP4).freeze
- EMPTY_FRACTION =
[].freeze
Class Method Summary collapse
Class Method Details
.split_by_unit4(num) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/ya_kansuji/formatter.rb', line 13 def split_by_unit4(num) chunks = [] UNIT4_CHUNK_LIMIT.times do num, chunk = num.divmod(UNIT4_BASE) chunks << chunk break if num.zero? end chunks end |
.split_fraction(num) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ya_kansuji/formatter.rb', line 23 def split_fraction(num) num = YaKansuji.normalize_value(num) raise ArgumentError, 'Value must be non-negative' if num.negative? return [num, EMPTY_FRACTION] if num.is_a?(Integer) int = num.truncate scaled = ((num - int) * FRAC_BASE).to_i digits = [] UNIT_FRAC.size.times do scaled, d = scaled.divmod(10) digits.unshift(d) unless digits.empty? && d.zero? end [int, digits] end |