Class: ChineseCapital::Number

Inherits:
Object
  • Object
show all
Defined in:
lib/chinese_capital/number.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



7
8
9
# File 'lib/chinese_capital/number.rb', line 7

def config
  @config
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/chinese_capital/number.rb', line 17

def configure
  yield(config)
end

.parse(num) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chinese_capital/number.rb', line 21

def parse(num)
  result = []
  b_num = BigDecimal(num.to_s)

  normal_zero = config.normal[:figure][0]
  return normal_zero if b_num.zero?

  temp = b_num.abs.divmod(1)
  round_section = temp[0].to_s('F').to_i
  decimal_section = temp[1].to_s('F').split('.')[1]

  result_round_section = round_section_zh(round_section)
  result << result_round_section unless result_round_section.empty?
  result << normal_zero if temp[1] != 0 && result.empty?
  result << config.normal[:point_unit] unless temp[1].zero?
  result << decimal_section_zh(decimal_section) unless temp[1].zero?
  result.unshift(config.normal[:minus_unit]) if b_num < 0
  result.join
end

.resetObject



13
14
15
# File 'lib/chinese_capital/number.rb', line 13

def reset
  @config = Configuration.new
end

.to_money(num) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chinese_capital/number.rb', line 41

def to_money(num)
  result = []

  # 金额只保留前两位小数
  b_num = BigDecimal(num.to_s).truncate(2)

  money_zero = config.money[:figure][0]
  return "#{money_zero}#{config.money[:unit]}#{config.money[:round_unit]}" if b_num.zero?

  temp = b_num.abs.divmod(1)
  round_section = temp[0].to_s('F').to_i
  decimal_section = temp[1].to_s('F').split('.')[1]

  result_round_section = round_section_zh(round_section, 'money')
  result << result_round_section unless result_round_section.empty?
  result << config.money[:unit] unless result_round_section.empty?
  unless temp[1].zero?
    result_decimal_section = decimal_section_zh(decimal_section, 'money')
    result_decimal_section.gsub!(/(?<=\A|["#{money_zero}"])["#{money_zero}"]+/, '') if result.empty?
    result << result_decimal_section
  end
  result << config.money[:round_unit] if BigDecimal(decimal_section).zero?
  result.unshift(config.money[:minus_unit]) if b_num < 0 && result
  result.join
end