bangou 1.0 → 1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +2 -3
- data/Rakefile +1 -1
- data/lib/bangou.rb +29 -18
- data/lib/extensions/integer.rb +11 -0
- data/readme.md +9 -0
- data/spec/bangou_spec.rb +1 -1
- metadata +2 -1
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/lib/bangou.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
+
require 'extensions/integer.rb'
|
4
|
+
|
3
5
|
class Bangou
|
4
6
|
class OutOfRangeException < Exception; end
|
5
7
|
|
@@ -50,7 +52,7 @@ class Bangou
|
|
50
52
|
EXCEPTIONS = {}
|
51
53
|
|
52
54
|
ZEROES = {
|
53
|
-
:text => "
|
55
|
+
:text => "ゼロ",
|
54
56
|
:numeral => "0"
|
55
57
|
}
|
56
58
|
|
@@ -71,23 +73,32 @@ class Bangou
|
|
71
73
|
return ZEROES[format] if int == 0
|
72
74
|
digits = int.to_s.split(//).reverse
|
73
75
|
digits.each_with_index.map do |digit, power|
|
74
|
-
|
75
|
-
|
76
|
-
if value = exceptions(num * base, format)
|
77
|
-
value
|
78
|
-
else
|
79
|
-
if num > 0
|
80
|
-
if power > 4
|
81
|
-
text = exceptions(num * base/10000, format) || numerals(num, format) + bases(base/10000, format)
|
82
|
-
text += bases(10000, format) unless digits.size > 1 and (digits[0..power - 1] & ("1".."9").to_a).size > 0
|
83
|
-
else
|
84
|
-
text = numerals(num, format) + bases(base, format)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
text = text[1..-1] if text =~ /^(一).+/
|
88
|
-
text = text[2..-1] if text =~ /^(いち).+/
|
89
|
-
text
|
90
|
-
end
|
76
|
+
hide_base_from_value = has_more_digits_before_index?(digits, power)
|
77
|
+
characters_for_digit_in_base(digit.to_i, 10 ** power, format, hide_base_from_value)
|
91
78
|
end.reverse.join
|
92
79
|
end
|
80
|
+
|
81
|
+
def self.strip_unneeded_characters text
|
82
|
+
text = text[1..-1] if text =~ /^(一).+/
|
83
|
+
text = text[2..-1] if text =~ /^(いち).+/
|
84
|
+
text
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.has_more_digits_before_index?(digits, index)
|
88
|
+
digits.size > 1 and (digits[0..index - 1] & ("1".."9").to_a).size > 0
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.characters_for_digit_in_base(num, base, format, hide_base_from_value)
|
92
|
+
if text = exceptions(num * base, format)
|
93
|
+
text
|
94
|
+
else
|
95
|
+
if num > 0 and base > 10000
|
96
|
+
text = exceptions(num * base/10000, format) || characters_for_digit_in_base(num, base/10000, format, hide_base_from_value)
|
97
|
+
text += bases(10000, format) unless hide_base_from_value
|
98
|
+
elsif num > 0
|
99
|
+
text = numerals(num, format) + bases(base, format)
|
100
|
+
end
|
101
|
+
strip_unneeded_characters(text)
|
102
|
+
end
|
103
|
+
end
|
93
104
|
end
|
data/readme.md
CHANGED
@@ -2,11 +2,20 @@
|
|
2
2
|
|
3
3
|
A Ruby library for converting between positive integers and Sino-Japanese numbers or text.
|
4
4
|
|
5
|
+
[](Build Status)
|
6
|
+
|
5
7
|
## Usage
|
6
8
|
|
7
9
|
```
|
8
10
|
require 'bangou'
|
9
11
|
|
12
|
+
|
13
|
+
89.to_j
|
14
|
+
# => "八十九"
|
15
|
+
|
16
|
+
289.to_jtext
|
17
|
+
# => "二百八十九"
|
18
|
+
|
10
19
|
Bangou.integer_to_japanese_numerals(2305)
|
11
20
|
# => "二千三百五"
|
12
21
|
|
data/spec/bangou_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bangou
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.1'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- LICENSE
|
41
41
|
- readme.md
|
42
42
|
- lib/bangou.rb
|
43
|
+
- lib/extensions/integer.rb
|
43
44
|
- spec/bangou_spec.rb
|
44
45
|
homepage: http://kattrali.github.com/bangou
|
45
46
|
licenses: []
|