0 ~ 999 の数値から単語へのコンバーターを作成しましたが、1 つのバグを除いて、ほぼ完全に機能しています。
100 を超える特定の数では、「100」セクションと「10/単位」セクションの間にスペースがありません。
たとえば、864 は 'eth canspajar ha triugens' で、缶と pajar の間にスペースが必要です (100 = 缶)
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)));
}