-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ae05a4f
Showing
9 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp | ||
*.bundle | ||
*.so | ||
*.o | ||
*.a | ||
mkmf.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in chinese_numerals.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2014 Fox Chao | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# ChineseNumerals | ||
|
||
This simple monkey patching enables the conversion from Integer to Chinese Numerals easily. The single method `to_chinese(:simple => true, :decimal => false)` is the only thing you need to take care. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'chinese_numerals' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install chinese_numerals | ||
|
||
## Usage | ||
|
||
Check the `spec/chinese_numerals_spec.rb` to see the various outcomes. | ||
|
||
## Contributing | ||
|
||
1. Fork it ( https://github.com/[my-github-username]/chinese_numerals/fork ) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require "bundler/gem_tasks" | ||
|
||
require 'tasks/test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'chinese_numerals/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "chinese_numerals" | ||
spec.version = ChineseNumerals::VERSION | ||
spec.authors = ["Fox Chao"] | ||
spec.email = ["[email protected]"] | ||
spec.summary = %q{Convert integers to Chinese Numerals} | ||
spec.description = %q{A little tool from Steam Computing, Co., Inc.} | ||
spec.homepage = "https://rubygems.org/gems/chinese_numerals" | ||
spec.license = "MIT" | ||
|
||
spec.files = `git ls-files -z`.split("\x0") | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.6" | ||
spec.add_development_dependency "rake", "~> 10.3" | ||
spec.add_development_dependency "minitest", "~> 5.3" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# encoding: UTF-8 | ||
|
||
Integer.class_eval do | ||
CHINESE_GROUPS = [ '', '萬', '億', '兆', '京', '陔' ] | ||
|
||
FORMAL_CHINESE_NUMBERS = %w{ 零 壹 貳 參 肆 伍 陸 柒 捌 玖 } | ||
FORMAL_CHINESE_DECIMALS = [ '', '拾', '佰', '仟' ] | ||
|
||
SIMPLE_CHINESE_NUMBERS = %w{ 零 一 二 三 四 五 六 七 八 九 } | ||
SIMPLE_CHINESE_DECIMALS = [ '', '十', '百', '千' ] | ||
|
||
def to_chinese(args = {}) | ||
opts = { | ||
:simple => true, | ||
:decimal => false | ||
}.merge args | ||
|
||
chinese_numbers = opts[:simple] ? SIMPLE_CHINESE_NUMBERS : FORMAL_CHINESE_NUMBERS | ||
chinese_decimals = opts[:simple] ? SIMPLE_CHINESE_DECIMALS : FORMAL_CHINESE_DECIMALS | ||
|
||
str = self.to_s | ||
len = str.length | ||
res = "" | ||
|
||
if opts[:decimal] && (len > 1) | ||
# counting zeros if it meets | ||
zeros = 0 | ||
|
||
0.upto len - 1 do |i| | ||
digit = str[i].to_i | ||
if digit == 0 | ||
zeros += 1 | ||
else | ||
if (zeros > 0) | ||
zeros = 0 | ||
res << chinese_numbers[0] | ||
end | ||
res << chinese_numbers[digit] + chinese_decimals[(len - 1 - i) % 4] | ||
end | ||
# append group name unless 4 zeros met | ||
if (len - 1 - i) % 4 == 0 | ||
res << CHINESE_GROUPS[(len - 1 - i) / 4] unless zeros == 4 | ||
# reset zeros for each group | ||
zeros = 0 | ||
end | ||
end | ||
|
||
else | ||
# simply maps each digit | ||
0.upto len - 1 do |i| | ||
digit = str[i].to_i | ||
res << chinese_numbers[digit] | ||
end | ||
end | ||
|
||
res | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ChineseNumerals | ||
VERSION = "0.0.2" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
$VERBOSE = true | ||
require 'minitest/autorun' | ||
require 'minitest/spec' | ||
|
||
require_relative '../lib/chinese_numerals.rb' | ||
|
||
describe Integer do | ||
it "maps each digit" do | ||
10000.to_chinese.must_equal("一零零零零") | ||
end | ||
|
||
it "maps each digit with formal Chinese digits" do | ||
10000.to_chinese(:simple => false).must_equal("壹零零零零") | ||
end | ||
|
||
it "emits zero in decimal mode" do | ||
0.to_chinese(:decimal => true).must_equal("零") | ||
end | ||
|
||
it "generates decimal notations in decimal mode" do | ||
12.to_chinese(:decimal => true).must_equal("一十二") | ||
end | ||
|
||
it "shortens middle zeros in decimal mode" do | ||
1002.to_chinese(:decimal => true).must_equal("一千零二") | ||
end | ||
|
||
it "omits the tail zero in decimal mode" do | ||
1200.to_chinese(:decimal => true).must_equal("一千二百") | ||
end | ||
|
||
it "omits all tail zeros in decimal mode" do | ||
10000000000.to_chinese(:decimal => true).must_equal("一百億") | ||
end | ||
|
||
it "knows output and omit zeros in decimal mode" do | ||
10000002000.to_chinese(:decimal => true).must_equal("一百億二千") | ||
end | ||
|
||
end | ||
|