从字符串中提取后,我需要将美国单词数字转换为数字。我在这里尝试了一下http://jsfiddle.net/golupant/fT2tk/
即我想将 10440 转换为它的等值数字——10440。
function word2numberConverter ( wordNumber) {
wordNumber = wordNumber.toLowerCase();
//different notations under hundred
var underTen = {
'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9
};
var underHundred = {
'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13,
'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20,
'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninety': 90
};
//different magnitude each after the increament 10 pow 3
var magnitude = {
'thousand': '10^3', 'million':'10^6', 'billion':'10^9', 'trillion':'10^12', 'quadrillion':'10^15',
'quintillion':'10^18', 'sextillion':'10^21', 'septillion':'10^24', 'octillion':'10^27', 'nonillion':'10^30',
'decillion':'10^33', 'undecillion':'10^36', 'duodecillion':'10^39', 'tredecillion':'10^42', 'quattuordecillion':'10^45',
'quindecillion':'10^48','sexdecillion':'10^51', 'septendecillion':'10^54', 'octadecillion':'10^57', 'novemdecillion':'10^60',
'vigintillion':'10^63',
};
//window.console&&console.log(wordNumber);
var wordnumElements = wordNumber.split(/[\s-]+/);
//alert('++++'+wordnumElements);
var num = 0;
var tempnum = 0;
var isUnderHundred = 0;
var isHundred = 0;
var isMagnitude = 0;
var isUnderTen = 0;
var isValidNum = 1;
var element = '';
var wordnumElementsLength = wordnumElements.length;
for (var i=0;i < wordnumElementsLength; i++) {
element = wordnumElements[i];
window.console&&console.log(element);
//wordnumElements.forEach(function( element ) {
if(!isValidNum) {
//if invalid number then return
break;
}
var temp = underTen[element];
if(temp != null) {
if(isUnderTen) {
//can't be a valid number
isValidNum = 0;
} else {
tempnum = tempnum + temp;
isUnderTen =1;
//reset isHundred , isUnderHundred and isMagnitude
isHundred = 0;
isUnderHundred = 0;
isMAgnitude = 0;
}
} else {
var temp = underHundred[element];
if(temp != null) {
if(isUnderHundred){
//can't be a valid number
isValidNum = 0;
} else {
tempnum = tempnum + temp;
isUnderHundred = 1;
//reset isHundred , isUnderTen and isMagnitude
isUnderTen = 0;
isHundred = 0;
isMagnitude = 0;
}
} else {
if ( element == "hundred" ) {
if(isHundred || isUnderHundred) { // there can never be underHundred string before valid american wordnumber like one thousand forty hundred is invalid
//not a valid number
isValidNum = 0;
} else {
isHundred =1;
//reset underHundred and isMagnitude
isUnderHundred = 0;
isMagnitude = 0;
isUnderTen = 0;
}
if(!tempnum) { // to give support for one thousand hundred forty as 1140
tempnum = 1;
}
tempnum = tempnum *100;
} else {
temp = magnitude[element];
if(temp ){
if(isMagnitude == 1) {
//not a valid number
isValidNum = 0;
} else {
isMagnitude = 1;
//reset isUnderHundred and isHundred
isUnderHundred = 0;
isHundred = 0;
isUnderTen = 0;
}
var val = temp.split(/\^/);
temp = Math.pow(val[0],val[1]);
window.console&&console.log('num--'+num);
window.console&&console.log('tempnum--'+tempnum);
window.console&&console.log('temp--'+temp)
if( temp != null) {
if(num && isMagnitude && temp > num) {
window.console&&console.log('i m --> num * temp + tempnum===>'+num+'--'+temp+'--'+tempnum);
num = (num + tempnum)* temp;
} else {
window.console&&console.log('i m addition--> num + tempnum * temp===>'+num+'--'+tempnum+'--'+temp);
num = num + tempnum * temp;
}
tempnum = 0;
} else {
isValidWordNum = 0;
}
}
}
}
}
window.console&&console.log('enum--'+num);
window.console&&console.log('etempnum--'+tempnum);
window.console&&console.log('etemp--'+temp);
}
num = num + tempnum;
alert("final num-"+num);
if(!isValidNum) {
return wordNumber;
} else {
return num;
}
}
http://jsfiddle.net/golupant/LFrm7/12/ - 如果数字下降的话,效果很好。意味着最高的幅度首先出现然后更小......
在http://jsfiddle.net/golupant/fT2tk/ 我试图照顾像这样的数字
“二亿五百万四百亿一千七百五十”
我一直在工作,直到一些现存但不完全正确。我只想像谷歌那样做。
请做谷歌“2.5亿401755-1”。你会知道我想要什么。
有人可以帮我做这件事吗?虽然我想要使用 javascript,但我也可以使用其他语言。