Class: Libruby::UnitConverterUtils
- Inherits:
-
Object
- Object
- Libruby::UnitConverterUtils
- Defined in:
- lib/libruby/unit_converter.rb
Class Method Summary collapse
- .comma(str) ⇒ Object
- .ja(str) ⇒ Object
- .to_comma_s(num) ⇒ Object
- .to_ja_s(num) ⇒ Object
- .to_s(num) ⇒ Object
Class Method Details
.comma(str) ⇒ Object
14 15 16 17 |
# File 'lib/libruby/unit_converter.rb', line 14 def self.comma(str) #http://rubytips86.hatenablog.com/entry/2014/03/28/153843 str.gsub(/(\d)(?=\d{3}+$)/, '\\1,') #=> "1,234,567,890" end |
.ja(str) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/libruby/unit_converter.rb', line 30 def self.ja(str) units = %w(万 億 兆 京 垓 予 穣 溝 澗 正 裁 極 恒河沙 阿僧祇 那由多 不可思議 無量大数 洛叉 倶胝) numbers = [] str.each_char {|num| numbers << num.to_i } # 1234567 number_blocks = numbers.reverse.each_slice(4).to_a # [7, 6, 5, 4], [3, 2, 1], result = '' number_blocks.each_with_index do |block, index| u = index > 0 ? units[index - 1] : '' result = block.reverse.join + u + result end result.sub!(/万0+/, '万') result end |
.to_comma_s(num) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/libruby/unit_converter.rb', line 19 def self.to_comma_s(num) s = to_s(num) result = nil if s =~ /(\d+)(\.\d+)/ result = comma($1) + $2 else result = comma(s) end result end |
.to_ja_s(num) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/libruby/unit_converter.rb', line 47 def self.to_ja_s(num) s = to_s(num) if s =~ /(\d+)(\.\d+)/ result = ja($1) + $2 else result = ja(s) end result end |
.to_s(num) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/libruby/unit_converter.rb', line 7 def self.to_s(num) #https://stackoverflow.com/questions/18533026/trim-a-trailing-0 # 単なる数値として返す。ただし少数以下のゼロは削除。 i, f = num.to_i, num.to_f i == f ? i.to_s : f.to_s end |