chinese_numbers_characters_converter 1.2.0 → 1.3.0
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.
- checksums.yaml +5 -5
- data/README.md +55 -10
- data/lib/chinese_number/version.rb +5 -0
- data/lib/chinese_number.rb +80 -0
- metadata +18 -32
- data/.gitignore +0 -9
- data/.travis.yml +0 -4
- data/CODE_OF_CONDUCT.md +0 -13
- data/Gemfile +0 -4
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -7
- data/chinese_numbers_characters_converter-0.1.0.gem +0 -0
- data/chinese_numbers_characters_converter-1.1.0.gem +0 -0
- data/chinese_numbers_characters_converter.gemspec +0 -34
- data/lib/chinese_numbers_characters_converter/version.rb +0 -3
- data/lib/chinese_numbers_characters_converter.rb +0 -27
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6dcea4637ba07d9b2924bbb04b4391e5b9891befe4a95801cf7b74e31f807b8c
|
|
4
|
+
data.tar.gz: 9ade796d8df232030ff33ed1fb39c337c4b189261df10a54a4ff06e59b76e121
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 76776616ef8e76b25823863312fc2ef49ef4a2b176c2d65a64cbedff1c56e0995ef5aca788081ec145c600b707655a61ca850e44f78d79dc028c221388440f9c
|
|
7
|
+
data.tar.gz: 3cce8c0ec575afcc263ee3855d642dc10a076a4df8dcc16e02b604337c2ad0ac96ce02dd91355140650ea5cbd3b50582433d47ee3ff4bfcf812ef65ab7763f43
|
data/README.md
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ChineseNumber
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
|
3
|
+
Convert integers to Traditional Chinese financial numerals, such as the
|
|
4
|
+
uppercase characters commonly used on Taiwan banking and accounting documents.
|
|
6
5
|
|
|
7
6
|
## Installation
|
|
8
7
|
|
|
9
8
|
Add this line to your application's Gemfile:
|
|
10
9
|
|
|
11
10
|
```ruby
|
|
12
|
-
gem '
|
|
11
|
+
gem 'chinese_number'
|
|
13
12
|
```
|
|
14
13
|
|
|
15
14
|
And then execute:
|
|
@@ -18,13 +17,60 @@ And then execute:
|
|
|
18
17
|
|
|
19
18
|
Or install it yourself as:
|
|
20
19
|
|
|
21
|
-
$ gem install
|
|
20
|
+
$ gem install chinese_number
|
|
22
21
|
|
|
23
22
|
## Usage
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
```ruby
|
|
25
|
+
require 'chinese_number'
|
|
26
|
+
|
|
27
|
+
ChineseNumber.convert(2_131_231)
|
|
28
|
+
# => "貳佰壹拾參萬壹仟貳佰參拾壹元整"
|
|
29
|
+
|
|
30
|
+
ChineseNumber.convert('0010001')
|
|
31
|
+
# => "壹萬零壹元整"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`nil` returns `nil`. Non-integer input raises `ArgumentError`.
|
|
35
|
+
|
|
36
|
+
## Ruby on Rails
|
|
26
37
|
|
|
27
|
-
|
|
38
|
+
Add the gem to your Rails application's `Gemfile`:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
gem 'chinese_number'
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Then install it:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
bundle install
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Rails loads gems from the bundle automatically, so you can call it from models,
|
|
51
|
+
controllers, helpers, jobs, or views:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
ChineseNumber.convert(10_001)
|
|
55
|
+
# => "壹萬零壹元整"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Example controller usage:
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
class InvoicesController < ApplicationController
|
|
62
|
+
def show
|
|
63
|
+
@invoice = Invoice.find(params[:id])
|
|
64
|
+
@total_in_chinese = ChineseNumber.convert(@invoice.total_cents / 100)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Example view usage:
|
|
70
|
+
|
|
71
|
+
```erb
|
|
72
|
+
<%= ChineseNumber.convert(@invoice.total_amount) %>
|
|
73
|
+
```
|
|
28
74
|
|
|
29
75
|
## Development
|
|
30
76
|
|
|
@@ -34,10 +80,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
34
80
|
|
|
35
81
|
## Contributing
|
|
36
82
|
|
|
37
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jct808/chinese_number. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://www.contributor-covenant.org/) code of conduct.
|
|
38
84
|
|
|
39
85
|
|
|
40
86
|
## License
|
|
41
87
|
|
|
42
88
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
43
|
-
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "chinese_number/version"
|
|
4
|
+
|
|
5
|
+
module ChineseNumber
|
|
6
|
+
def self.convert(input_num)
|
|
7
|
+
Converter.run(input_num)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class Converter
|
|
11
|
+
NUMBERS = %w[零 壹 貳 參 肆 伍 陸 柒 捌 玖].freeze
|
|
12
|
+
DIGIT_UNITS = ["", "拾", "佰", "仟"].freeze
|
|
13
|
+
SECTION_UNITS = ["", "萬", "億", "兆", "京"].freeze
|
|
14
|
+
INTEGER_PATTERN = /\A[+-]?\d+\z/.freeze
|
|
15
|
+
|
|
16
|
+
def self.run(input_num)
|
|
17
|
+
return unless input_num
|
|
18
|
+
|
|
19
|
+
normalized = normalize(input_num)
|
|
20
|
+
sign = normalized.delete_prefix!("-") ? "負" : ""
|
|
21
|
+
normalized.delete_prefix!("+")
|
|
22
|
+
normalized = normalized.sub(/\A0+/, "")
|
|
23
|
+
|
|
24
|
+
return "#{sign}零元整" if normalized.empty?
|
|
25
|
+
|
|
26
|
+
groups = normalized.reverse.scan(/\d{1,4}/).map { |group| group.reverse.to_i }
|
|
27
|
+
raise ArgumentError, "number is too large to convert" if groups.length > SECTION_UNITS.length
|
|
28
|
+
|
|
29
|
+
converted_groups = groups.each_with_index.map do |group, index|
|
|
30
|
+
next if group.zero?
|
|
31
|
+
|
|
32
|
+
group_to_chinese(group) + SECTION_UNITS[index]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
result = []
|
|
36
|
+
pending_zero = false
|
|
37
|
+
|
|
38
|
+
converted_groups.reverse.each_with_index do |group, index|
|
|
39
|
+
original_group = groups.reverse[index]
|
|
40
|
+
|
|
41
|
+
if group
|
|
42
|
+
result << NUMBERS[0] if pending_zero || (index.positive? && original_group < 1000)
|
|
43
|
+
result << group
|
|
44
|
+
pending_zero = false
|
|
45
|
+
elsif result.any?
|
|
46
|
+
pending_zero = true
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
"#{sign}#{result.join}元整"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.normalize(input_num)
|
|
54
|
+
normalized = input_num.to_s.strip
|
|
55
|
+
raise ArgumentError, "input must be an integer" unless normalized.match?(INTEGER_PATTERN)
|
|
56
|
+
|
|
57
|
+
normalized
|
|
58
|
+
end
|
|
59
|
+
private_class_method :normalize
|
|
60
|
+
|
|
61
|
+
def self.group_to_chinese(group)
|
|
62
|
+
result = []
|
|
63
|
+
pending_zero = false
|
|
64
|
+
|
|
65
|
+
group.digits.each_with_index.reverse_each do |digit, index|
|
|
66
|
+
if digit.zero?
|
|
67
|
+
pending_zero = true if result.any?
|
|
68
|
+
next
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
result << NUMBERS[0] if pending_zero
|
|
72
|
+
result << NUMBERS[digit] + DIGIT_UNITS[index]
|
|
73
|
+
pending_zero = false
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
result.join
|
|
77
|
+
end
|
|
78
|
+
private_class_method :group_to_chinese
|
|
79
|
+
end
|
|
80
|
+
end
|
metadata
CHANGED
|
@@ -1,85 +1,73 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: chinese_numbers_characters_converter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chris Tai
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bundler
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
18
|
version: '1.10'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - "
|
|
23
|
+
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
25
|
version: '1.10'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: rake
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- - "
|
|
30
|
+
- - ">="
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
32
|
version: '10.0'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- - "
|
|
37
|
+
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
39
|
version: '10.0'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
41
|
name: minitest
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
|
-
- - "
|
|
44
|
+
- - ">="
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: '0'
|
|
46
|
+
version: '5.0'
|
|
48
47
|
type: :development
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
|
-
- - "
|
|
51
|
+
- - ">="
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: '0'
|
|
55
|
-
description:
|
|
56
|
-
|
|
53
|
+
version: '5.0'
|
|
54
|
+
description: Converts integer amounts to Traditional Chinese financial numerals commonly
|
|
55
|
+
used on Taiwan banking and accounting documents.
|
|
57
56
|
email:
|
|
58
57
|
- jct808@gmail.com
|
|
59
58
|
executables: []
|
|
60
59
|
extensions: []
|
|
61
60
|
extra_rdoc_files: []
|
|
62
61
|
files:
|
|
63
|
-
- ".gitignore"
|
|
64
|
-
- ".travis.yml"
|
|
65
|
-
- CODE_OF_CONDUCT.md
|
|
66
|
-
- Gemfile
|
|
67
62
|
- LICENSE.txt
|
|
68
63
|
- README.md
|
|
69
|
-
-
|
|
70
|
-
-
|
|
71
|
-
|
|
72
|
-
- chinese_numbers_characters_converter-0.1.0.gem
|
|
73
|
-
- chinese_numbers_characters_converter-1.1.0.gem
|
|
74
|
-
- chinese_numbers_characters_converter.gemspec
|
|
75
|
-
- lib/chinese_numbers_characters_converter.rb
|
|
76
|
-
- lib/chinese_numbers_characters_converter/version.rb
|
|
77
|
-
homepage: https://rubygems.org
|
|
64
|
+
- lib/chinese_number.rb
|
|
65
|
+
- lib/chinese_number/version.rb
|
|
66
|
+
homepage: https://github.com/jct808/chinese_number
|
|
78
67
|
licenses:
|
|
79
68
|
- MIT
|
|
80
69
|
metadata:
|
|
81
70
|
allowed_push_host: https://rubygems.org
|
|
82
|
-
post_install_message:
|
|
83
71
|
rdoc_options: []
|
|
84
72
|
require_paths:
|
|
85
73
|
- lib
|
|
@@ -87,16 +75,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
87
75
|
requirements:
|
|
88
76
|
- - ">="
|
|
89
77
|
- !ruby/object:Gem::Version
|
|
90
|
-
version: '
|
|
78
|
+
version: '2.5'
|
|
91
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
80
|
requirements:
|
|
93
81
|
- - ">="
|
|
94
82
|
- !ruby/object:Gem::Version
|
|
95
83
|
version: '0'
|
|
96
84
|
requirements: []
|
|
97
|
-
|
|
98
|
-
rubygems_version: 2.4.6
|
|
99
|
-
signing_key:
|
|
85
|
+
rubygems_version: 4.0.14
|
|
100
86
|
specification_version: 4
|
|
101
|
-
summary:
|
|
87
|
+
summary: Convert integers to Traditional Chinese financial numerals.
|
|
102
88
|
test_files: []
|
data/.gitignore
DELETED
data/.travis.yml
DELETED
data/CODE_OF_CONDUCT.md
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# Contributor Code of Conduct
|
|
2
|
-
|
|
3
|
-
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
|
4
|
-
|
|
5
|
-
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
|
6
|
-
|
|
7
|
-
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
|
8
|
-
|
|
9
|
-
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
|
10
|
-
|
|
11
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
|
12
|
-
|
|
13
|
-
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
DELETED
data/Rakefile
DELETED
data/bin/console
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
require "bundler/setup"
|
|
4
|
-
require "chinese_numbers_characters_converter"
|
|
5
|
-
|
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
-
|
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
-
# require "pry"
|
|
11
|
-
# Pry.start
|
|
12
|
-
|
|
13
|
-
require "irb"
|
|
14
|
-
IRB.start
|
data/bin/setup
DELETED
|
Binary file
|
|
Binary file
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require 'chinese_numbers_characters_converter/version'
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |spec|
|
|
7
|
-
spec.name = "chinese_numbers_characters_converter"
|
|
8
|
-
spec.version = ChineseNumbersCharactersConverter::VERSION
|
|
9
|
-
spec.authors = ["Chris Tai"]
|
|
10
|
-
spec.email = ["jct808@gmail.com"]
|
|
11
|
-
|
|
12
|
-
spec.summary = %q{To Convert a Integer to Chinese Number Characters.}
|
|
13
|
-
spec.description = %q{In Taiwan, Bank or other finance facility are using Chinese numerals character substitute for Arabic numerals, because of security and correctly reason.}
|
|
14
|
-
spec.homepage = "https://rubygems.org"
|
|
15
|
-
spec.license = "MIT"
|
|
16
|
-
|
|
17
|
-
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
|
18
|
-
# delete this section to allow pushing this gem to any host.
|
|
19
|
-
if spec.respond_to?(:metadata)
|
|
20
|
-
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
|
21
|
-
else
|
|
22
|
-
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
26
|
-
spec.bindir = "exe"
|
|
27
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
|
-
spec.require_paths = ["lib"]
|
|
29
|
-
|
|
30
|
-
spec.add_development_dependency "bundler", "~> 1.10"
|
|
31
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
|
32
|
-
spec.add_development_dependency 'minitest', '~> 0'
|
|
33
|
-
|
|
34
|
-
end
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
require "chinese_numbers_characters_converter/version"
|
|
2
|
-
|
|
3
|
-
module ChineseNumbersCharactersConverter
|
|
4
|
-
class Converter
|
|
5
|
-
|
|
6
|
-
def self.run(str)
|
|
7
|
-
number = ["零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖"]
|
|
8
|
-
dig = ["", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟"]
|
|
9
|
-
@string_a = Array.new
|
|
10
|
-
@num_o = str
|
|
11
|
-
num_s = str.to_s
|
|
12
|
-
num_s = num_s.reverse!
|
|
13
|
-
for i in 0..(num_s.to_s).length-1
|
|
14
|
-
if ((num_s[i]).to_i != 0)
|
|
15
|
-
@string_a.push(number[(num_s[i]).to_i] + dig[i])
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
if @string_a.any?
|
|
19
|
-
@string_a = @string_a.reverse!()
|
|
20
|
-
@string_a.push("元整")
|
|
21
|
-
end
|
|
22
|
-
@num_o = num_s.reverse!
|
|
23
|
-
return @string_a.join
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
end
|
|
27
|
-
end
|