我已经为 0-999 创建了一个数字到单词的转换器,并且除了一个错误之外,它几乎可以完美地工作。
在某些超过一百的数字中,“百”部分和“十/单位”部分之间缺少空格。
例如 864 给出“eth canspajar ha tri ugens”,其中罐和 pajar 之间需要有空间(百 = 罐)
这似乎是一个常规问题,因为它发生在 164、264、364 等以及其他数字的范围内。
任何对 Javascript 或数学有更好了解的人都可以看看我的代码,看看他们是否能发现模式或解决方案?该代码取自我没有制作的法语数字转换器,而且我对 Javascript 相对较新,因此我很难完全理解那里的所有内容。
您可以在下面或http://jsfiddle.net/nickykw/tMXmj/2/上查看代码
非常感谢
function num2Letters(number) {
if (isNaN(number) || number < 0 || 999 < number) {
return 'Veuillez entrer un nombre entier compris entre 0 et 999.';
}
var units2Letters = ['', 'onen', 'dew', 'tri', 'pajar', 'pemp', 'whegh', 'seyth', 'eth', 'naw', 'deg', 'udnek', 'dowdhek', 'tredhek', 'peswardhek', 'pemthek', 'whetek', 'seytek', 'etek', 'nownsek'],
tens2Letters = ['', 'deg', 'ugens', 'warn ugens', 'dew ugens', 'ha dew ugens', 'tri ugens', 'ha tri ugens', 'pajar ugens', 'ha pajar ugens'];
twenties2Letters = ['', 'deg', 'ugens', 'warn ugens', 'dew ugens', 'ha dew ugens', 'tri ugens', 'ha tri ugens', 'pajar ugens', 'ha pajar ugens', 'pemp ugens', 'ha pemp ugens', 'whegh ugens', 'ha whegh ugens', 'seyth ugens', 'ha seyth ugens', 'eth ugens', 'hag eth ugens', 'naw ugens', 'ha naw ugens'];
hundreds2Letters = ['', 'cans', 'dew cans', 'tri hans', 'pajar cans', 'pemp cans', 'whegh cans', 'seyth cans', 'eth cans', 'naw cans'];
var units = number % 10,
tens = (number % 100 - units) / 10,
twenties = (number % 200 - units) / 10,
hundreds = (number % 1000 - number % 100) / 100;
var unitsOut, tensOut, twentiesOut, hundredsOut;
if (number === 0) {
return 'mann';
} else {
// THE UNITS
unitsOut = units2Letters[units];
// THE TENS
if (tens === 1 && units > 0) {
tensOut = units2Letters[10 + units];
unitsOut = '';
} else if (tens === 2 && units !== 0 ) {
tensOut = units2Letters[units] + ' warn ' + tens2Letters[tens];
unitsOut = '';
} else if (tens === 3) {
tensOut = units2Letters[10 + units] + ' ' + tens2Letters[tens];
unitsOut = '';
} else if ((tens === 4 && units !== 0) || (tens === 6 && units !== 0) || (tens === 8 && units !== 0)) {
tensOut = units2Letters[units] + ' ha ' + tens2Letters[tens];
unitsOut = '';
} else if (tens === 5 || tens === 7 || tens === 9) {
tensOut = units2Letters[10 + units] + ' ' + tens2Letters[tens];
unitsOut = '';
} else {
tensOut = tens2Letters[tens];
}
// THE TWENTIES - used only for 120-199
if ((number >= 121 && number <= 199) && (tens === 2 || tens === 4 || tens === 8) && units > 0) {
twentiesOut = units2Letters[units] + ' ha ' + twenties2Letters[twenties];
} else if ((number >= 121 && number <= 199) && (tens === 3 || tens === 5 || tens === 7 || tens === 9) ) {
twentiesOut = units2Letters[10 + units] + ' ' + twenties2Letters[twenties];
} else if ((number >= 121 && number <= 199) && (tens === 6 ) ) {
twentiesOut = units2Letters[units] + ' hag ' + twenties2Letters[twenties];
} else {
twentiesOut = twenties2Letters[twenties];
}
// THE HUNDREDS
//if the number is x01-09 or number is x1x or number is x2x or number is x3x then add a hag/ha after the number (hag if the number is xx1 or xx8 or x20)
if ((hundreds >= 1 && tens === 0 && units >= 1) || hundreds >= 1 && tens === 1 || hundreds >= 1 && tens === 2 || hundreds >= 1 && tens === 3) {
hundredsOut = hundreds2Letters[hundreds] + (hundreds >= 1 && (units === 1 || units === 8) || hundreds >= 1 && tens === 2 && units === 0 ? ' hag ' : ' ha ');
// insert a ha for 40, 60 and 80 above 200
} else if (hundreds >= 2 && (tens === 4 || tens === 6 || tens === 8) && units === 0) {
hundredsOut = hundreds2Letters[hundreds] + ' ha ';
} else {
hundredsOut = hundreds2Letters[hundreds];
}
// GET TOTAL
if (number === 50) {
return hundredsOut + (hundredsOut && tensOut ? ' ': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut + ' <i>or</i> hanter cans';
} else if (number === 150) {
return hundredsOut + (hundredsOut && tensOut ? ' ': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut + ' <i>or</i> deg ha seyth ugens <i>or</i> onen cans ha hanter';
} else if (hundreds > 1 && tens === 5 && units === 0 ) {
return hundredsOut + (hundredsOut && tensOut ? ' ': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut + ' <i>or</i> ' + hundredsOut + ' ha hanter';
} else if (hundreds === 1 && (tens === 2 || tens === 3 || tens === 4 || tens === 5 || tens === 6 || tens === 7 || tens === 8 || tens === 9) ) {
return twentiesOut + ' <i>or</i> ' + hundredsOut + (hundredsOut && tensOut ? '': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut;
} else {
return hundredsOut + (hundredsOut && tensOut ? '': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut;
}
}
}
var userEntry;
while (userEntry = prompt('Enter a number between 0 and 999:')) {
alert(num2Letters(parseInt(userEntry, 10)));
}