Created
August 7, 2012 01:05
-
-
Save yulanggong/3280233 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
var cnNum2ArabNum = function(cn){ | |
var arab, parts, cnChars = '零一二三四五六七八九' | |
if (!cn) { | |
return 0 | |
} | |
if (cn.indexOf('亿') !== -1){ | |
parts = cn.split('亿') | |
return cnNum2ArabNum(parts[0]) * 1e8 + cnNum2ArabNum(parts[1]) | |
} | |
if (cn.indexOf('万') !== -1){ | |
parts = cn.split('万') | |
return cnNum2ArabNum(parts[0]) * 1e4 + cnNum2ArabNum(parts[1]) | |
} | |
if (cn.indexOf('十') === 0){ | |
cn = '一' + cn | |
} | |
arab = cn | |
.replace(/[零一二三四五六七八九]/g, function (a) { | |
return '+' + cnChars.indexOf(a) | |
}) | |
.replace(/(十|百|千)/g, function(a, b){ | |
return '*' + ( | |
b == '十' ? 1e1 : | |
b == '百' ? 1e2 : 1e3 | |
) | |
}) | |
return (new Function('return ' + arab))() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment