module NumHelper
Constants
- DEL
- DELIMITER_REGEX
- NUM
- SUB_DEL
- UNIT
Public Instance Methods
to_parts(num_str, unit: [], del: [])
click to toggle source
# File lib/rails_com/utils/num_helper.rb, line 22 def to_parts(num_str, unit: [], del: []) han_arr = num_str.split(',').reverse.map.with_index do |str, index| str = str.each_char.map { |i| NUM[i.to_i] } xx = '' str.zip(del[0..str.size - 1].reverse) do |st, de| if st != '零' && de xx << st + de else xx << st unless xx.end_with?('零') end end if xx.chomp!('零') xx << (unit[index].to_s) << '零' else xx << (unit[index].to_s) end xx end han_arr.reverse.join end
to_rmb(num)
click to toggle source
# File lib/rails_com/utils/num_helper.rb, line 9 def to_rmb(num) left, right = num.to_s(:rounded, precision: 2, strip_insignificant_zeros: true, separator: '.', delimiter: ',', delimiter_pattern: DELIMITER_REGEX).split('.') left_str = to_parts(left, unit: UNIT, del: DEL) if right right_str = to_parts(right, del: SUB_DEL) left_str + right_str else left_str + '整' end end