Created
March 2, 2014 12:04
-
-
Save vangie/9305600 to your computer and use it in GitHub Desktop.
人民币小写转换为大写
This file contains hidden or 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
#! /usr/bin/env coffee | |
# convert amount from figures to words | |
# @author vangie.du | |
# @url http://codelife.me/blog/2013/03/09/convert-amount-from-figures-to-words-by-coffeescript/ | |
# @version 1.1 | |
# @since 2013-03-09 | |
if process.argv.length >= 3 | |
n = parseFloat(process.argv[2]).toFixed(2) | |
else | |
console.log " | |
Usage: \n | |
rmb figure_number\n | |
Example:\n | |
rmb 42342.33\n | |
肆万贰仟叁佰肆拾贰元叁角叁分" | |
process.exit(1) | |
fractions = ["角", "分"] | |
digits = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"] | |
units = [["元", "万", "亿"],["", "拾", "佰", "仟"]] | |
[head, n] = if n < 0 then ['负', -n] else ['', n] | |
s = '' | |
fractionalPart = Math.round(n * 100) % 100 | |
for i in [0..1] | |
s += (digits[Math.floor(fractionalPart / Math.pow(10, 1-i)) % 10] + fractions[i]) | |
.replace(/(零.)+/g, "") | |
s = '整' unless s | |
intPart = Math.floor(n) | |
for i in [0..units[0].length - 1] when intPart > 0 | |
p = '' | |
for j in [0..units[1].length - 1] when n > 0 | |
p = digits[intPart % 10] + units[1][j] + p | |
intPart = Math.floor(intPart / 10) | |
s = p.replace(/(零.)*零$/g, "").replace(/^$/g, "零") + units[0][i] + s | |
console.log(head + s.replace(/(零.)*零元/g, "元") | |
.replace(/(零.)+/, "") | |
.replace(/(零.)+/g, "零") | |
.replace(/^整$/g, "零元整")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment