使用 JavaScript 将数字转换为单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14766951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert digits into words with JavaScript
提问by Jeo
I am making a code which converts the given amount into words, heres is what I have got after googling. But I think its a little lengthy code to achieve a simple task.
Two Regular Expressions and two forloops, I want something simpler.
我正在制作一个将给定数量转换为单词的代码,这是我在谷歌搜索后得到的。但我认为它是一个有点冗长的代码来实现一个简单的任务。两个正则表达式和两个for循环,我想要更简单的东西。
I am trying to make it as shorter as possible. and will post what I come up with
我试图让它尽可能短。并将发布我想出的东西
Any suggestions?
有什么建议?
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
function toWords(s) {
s = s.toString();
s = s.replace(/[\, ]/g,'');
if (s != parseFloat(s)) return 'not a number';
var x = s.indexOf('.');
if (x == -1)
x = s.length;
if (x > 15)
return 'too big';
var n = s.split('');
var str = '';
var sk = 0;
for (var i=0; i < x; i++) {
if ((x-i)%3==2) {
if (n[i] == '1') {
str += tn[Number(n[i+1])] + ' ';
i++;
sk=1;
} else if (n[i]!=0) {
str += tw[n[i]-2] + ' ';
sk=1;
}
} else if (n[i]!=0) { // 0235
str += dg[n[i]] +' ';
if ((x-i)%3==0) str += 'hundred ';
sk=1;
}
if ((x-i)%3==1) {
if (sk)
str += th[(x-i-1)/3] + ' ';
sk=0;
}
}
if (x != s.length) {
var y = s.length;
str += 'point ';
for (var i=x+1; i<y; i++)
str += dg[n[i]] +' ';
}
return str.replace(/\s+/g,' ');
}
Also, the above code converts to English numbering system like Million/Billion, I wan't South Asian numbering system. like in Lakhs and Crores
此外,上面的代码转换为英文编号系统,如百万/十亿,我不想南亚编号系统。就像在十万和千万里
回答by Salman
Update: Looks like this is more useful than I thought. I've just published this on npm. https://www.npmjs.com/package/num-words
更新:看起来这比我想象的更有用。我刚刚在 npm 上发布了这个。https://www.npmjs.com/package/num-words
Here's a shorter code. with one RegEx and no loops. converts as you wanted, in south asian numbering system
这是一个较短的代码。有一个正则表达式,没有循环。在南亚编号系统中根据需要进行转换
var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
function inWords (num) {
if ((num = num.toString()).length > 9) return 'overflow';
n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!n) return; var str = '';
str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
return str;
}
document.getElementById('number').onkeyup = function () {
document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);
};
<span id="words"></span>
<input id="number" type="text" />
The only limitation is, you can convert maximum of 9 digits, which I think is more than sufficient in most cases..
唯一的限制是,您最多可以转换 9 位数字,我认为在大多数情况下这已经足够了。
回答by Thank you
"Deceptivelysimple task." – Potatoswatter
“看似简单的任务。” –土豆泥
Indeed. There's many little devils hanging out in the details of this problem. It was very fun to solve tho.
的确。在这个问题的细节中有很多小恶魔。解决这个问题很有趣。
EDIT:This update takes a much more compositional approach. Previously there was one big function which wrapped a couple other proprietary functions. Instead, this time we define generic reusable functions which could be used for many varieties of tasks. More about those after we take a look at numToWordsitself …
编辑:此更新采用了一种更具组合性的方法。以前有一个大函数包装了其他几个专有函数。相反,这一次我们定义了可用于多种任务的通用可重用函数。在我们审视numToWords自身之后,更多地了解这些……
// numToWords :: (Number a, String a) => a -> String
let numToWords = n => {
let a = [
'', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
];
let b = [
'', '', 'twenty', 'thirty', 'forty',
'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
];
let g = [
'', 'thousand', 'million', 'billion', 'trillion', 'quadrillion',
'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion'
];
// this part is really nasty still
// it might edit this again later to show how Monoids could fix this up
let makeGroup = ([ones,tens,huns]) => {
return [
num(huns) === 0 ? '' : a[huns] + ' hundred ',
num(ones) === 0 ? b[tens] : b[tens] && b[tens] + '-' || '',
a[tens+ones] || a[ones]
].join('');
};
// "thousands" constructor; no real good names for this, i guess
let thousand = (group,i) => group === '' ? group : `${group} ${g[i]}`;
// execute !
if (typeof n === 'number') return numToWords(String(n));
if (n === '0') return 'zero';
return comp (chunk(3)) (reverse) (arr(n))
.map(makeGroup)
.map(thousand)
.filter(comp(not)(isEmpty))
.reverse()
.join(' ');
};
Here are the dependencies:
以下是依赖项:
You'll notice these require next to no documentation because their intents are immediately clear. chunkmight be the only one that takes a moment to digest, but it's really not too bad. Plus the function name gives us a pretty good indication what it does, and it's probably a function we've encountered before.
您会注意到这些几乎不需要任何文档,因为它们的意图很明显。chunk可能是唯一需要一点时间来消化的,但确实还不错。加上函数名给了我们一个很好的指示它做什么,它可能是我们以前遇到过的一个函数。
const arr = x => Array.from(x);
const num = x => Number(x) || 0;
const str = x => String(x);
const isEmpty = xs => xs.length === 0;
const take = n => xs => xs.slice(0,n);
const drop = n => xs => xs.slice(n);
const reverse = xs => xs.slice(0).reverse();
const comp = f => g => x => f (g (x));
const not = x => !x;
const chunk = n => xs =>
isEmpty(xs) ? [] : [take(n)(xs), ...chunk (n) (drop (n) (xs))];
"So these make it better?"
“所以这些让它变得更好?”
Look at how the code has cleaned up significantly
看看代码是如何显着清理的
// NEW CODE (truncated)
return comp (chunk(3)) (reverse) (arr(n))
.map(makeGroup)
.map(thousand)
.filter(comp(not)(isEmpty))
.reverse()
.join(' ');
// OLD CODE (truncated)
let grp = n => ('000' + n).substr(-3);
let rem = n => n.substr(0, n.length - 3);
let cons = xs => x => g => x ? [x, g && ' ' + g || '', ' ', xs].join('') : xs;
let iter = str => i => x => r => {
if (x === '000' && r.length === 0) return str;
return iter(cons(str)(fmt(x))(g[i]))
(i+1)
(grp(r))
(rem(r));
};
return iter('')(0)(grp(String(n)))(rem(String(n)));
Most importantly, the utility functions we added in the new code can be used other places in your app. This means that, as a side effect of implementing numToWordsin this way, we get the other functions for free. Bonus soda !
最重要的是,我们在新代码中添加的实用函数可以在您的应用程序的其他地方使用。这意味着,作为numToWords以这种方式实现的副作用,我们可以免费获得其他功能。奖金苏打水!
Some tests
一些测试
console.log(numToWords(11009));
//=> eleven thousand nine
console.log(numToWords(10000001));
//=> ten million one
console.log(numToWords(987));
//=> nine hundred eighty-seven
console.log(numToWords(1015));
//=> one thousand fifteen
console.log(numToWords(55111222333));
//=> fifty-five billion one hundred eleven million two hundred
// twenty-two thousand three hundred thirty-three
console.log(numToWords("999999999999999999999991"));
//=> nine hundred ninety-nine sextillion nine hundred ninety-nine
// quintillion nine hundred ninety-nine quadrillion nine hundred
// ninety-nine trillion nine hundred ninety-nine billion nine
// hundred ninety-nine million nine hundred ninety-nine thousand
// nine hundred ninety-one
console.log(numToWords(6000753512));
//=> six billion seven hundred fifty-three thousand five hundred
// twelve
Runnable demo
可运行的演示
const arr = x => Array.from(x);
const num = x => Number(x) || 0;
const str = x => String(x);
const isEmpty = xs => xs.length === 0;
const take = n => xs => xs.slice(0,n);
const drop = n => xs => xs.slice(n);
const reverse = xs => xs.slice(0).reverse();
const comp = f => g => x => f (g (x));
const not = x => !x;
const chunk = n => xs =>
isEmpty(xs) ? [] : [take(n)(xs), ...chunk (n) (drop (n) (xs))];
// numToWords :: (Number a, String a) => a -> String
let numToWords = n => {
let a = [
'', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
];
let b = [
'', '', 'twenty', 'thirty', 'forty',
'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
];
let g = [
'', 'thousand', 'million', 'billion', 'trillion', 'quadrillion',
'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion'
];
// this part is really nasty still
// it might edit this again later to show how Monoids could fix this up
let makeGroup = ([ones,tens,huns]) => {
return [
num(huns) === 0 ? '' : a[huns] + ' hundred ',
num(ones) === 0 ? b[tens] : b[tens] && b[tens] + '-' || '',
a[tens+ones] || a[ones]
].join('');
};
let thousand = (group,i) => group === '' ? group : `${group} ${g[i]}`;
if (typeof n === 'number')
return numToWords(String(n));
else if (n === '0')
return 'zero';
else
return comp (chunk(3)) (reverse) (arr(n))
.map(makeGroup)
.map(thousand)
.filter(comp(not)(isEmpty))
.reverse()
.join(' ');
};
console.log(numToWords(11009));
//=> eleven thousand nine
console.log(numToWords(10000001));
//=> ten million one
console.log(numToWords(987));
//=> nine hundred eighty-seven
console.log(numToWords(1015));
//=> one thousand fifteen
console.log(numToWords(55111222333));
//=> fifty-five billion one hundred eleven million two hundred
// twenty-two thousand three hundred thirty-three
console.log(numToWords("999999999999999999999991"));
//=> nine hundred ninety-nine sextillion nine hundred ninety-nine
// quintillion nine hundred ninety-nine quadrillion nine hundred
// ninety-nine trillion nine hundred ninety-nine billion nine
// hundred ninety-nine million nine hundred ninety-nine thousand
// nine hundred ninety-one
console.log(numToWords(6000753512));
//=> six billion seven hundred fifty-three thousand five hundred
// twelve
You can transpile the code using babel.jsif you want to see the ES5 variant
如果您想查看 ES5 变体,可以使用babel.js转译代码
回答by McShaman
I spent a while developing a better solution to this. It can handle very big numbers but once they get over 16 digits you have pass the number in as a string. Something about the limit of JavaScript numbers.
我花了一段时间开发了一个更好的解决方案。它可以处理非常大的数字,但是一旦它们超过 16 位,您就将该数字作为字符串传入。关于 JavaScript 数字的限制。
function numberToEnglish( n ) {
var string = n.toString(), units, tens, scales, start, end, chunks, chunksLen, chunk, ints, i, word, words, and = 'and';
/* Remove spaces and commas */
string = string.replace(/[, ]/g,"");
/* Is number zero? */
if( parseInt( string ) === 0 ) {
return 'zero';
}
/* Array of units as words */
units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ];
/* Array of tens as words */
tens = [ '', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ];
/* Array of scales as words */
scales = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quatttuor-decillion', 'quindecillion', 'sexdecillion', 'septen-decillion', 'octodecillion', 'novemdecillion', 'vigintillion', 'centillion' ];
/* Split user arguemnt into 3 digit chunks from right to left */
start = string.length;
chunks = [];
while( start > 0 ) {
end = start;
chunks.push( string.slice( ( start = Math.max( 0, start - 3 ) ), end ) );
}
/* Check if function has enough scale words to be able to stringify the user argument */
chunksLen = chunks.length;
if( chunksLen > scales.length ) {
return '';
}
/* Stringify each integer in each chunk */
words = [];
for( i = 0; i < chunksLen; i++ ) {
chunk = parseInt( chunks[i] );
if( chunk ) {
/* Split chunk into array of individual integers */
ints = chunks[i].split( '' ).reverse().map( parseFloat );
/* If tens integer is 1, i.e. 10, then add 10 to units integer */
if( ints[1] === 1 ) {
ints[0] += 10;
}
/* Add scale word if chunk is not zero and array item exists */
if( ( word = scales[i] ) ) {
words.push( word );
}
/* Add unit word if array item exists */
if( ( word = units[ ints[0] ] ) ) {
words.push( word );
}
/* Add tens word if array item exists */
if( ( word = tens[ ints[1] ] ) ) {
words.push( word );
}
/* Add 'and' string after units or tens integer if: */
if( ints[0] || ints[1] ) {
/* Chunk has a hundreds integer or chunk is the first of multiple chunks */
if( ints[2] || ! i && chunksLen ) {
words.push( and );
}
}
/* Add hundreds word if array item exists */
if( ( word = units[ ints[2] ] ) ) {
words.push( word + ' hundred' );
}
}
}
return words.reverse().join( ' ' );
}
// - - - - - Tests - - - - - -
function test(v) {
var sep = ('string'==typeof v)?'"':'';
console.log("numberToEnglish("+sep + v.toString() + sep+") = "+numberToEnglish(v));
}
test(2);
test(721);
test(13463);
test(1000001);
test("21,683,200,000,621,384");
回答by Juan Gaitán
You might want to try it recursive. It works for numbers between 0 and 999999. Keep in mind that (~~)does the same as Math.floor
您可能想尝试递归。它适用于 0 到 999999 之间的数字。请记住(~~)与 Math.floor 的作用相同
var num = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" ");
var tens = "twenty thirty forty fifty sixty seventy eighty ninety".split(" ");
function number2words(n){
if (n < 20) return num[n];
var digit = n%10;
if (n < 100) return tens[~~(n/10)-2] + (digit? "-" + num[digit]: "");
if (n < 1000) return num[~~(n/100)] +" hundred" + (n%100 == 0? "": " " + number2words(n%100));
return number2words(~~(n/1000)) + " thousand" + (n%1000 != 0? " " + number2words(n%1000): "");
}
回答by Pramod Kharade
<html>
<head>
<title>HTML - Convert numbers to words using JavaScript</title>
<script type="text/javascript">
function onlyNumbers(evt) {
var e = event || evt; // For trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
function NumToWord(inputNumber, outputControl) {
var str = new String(inputNumber)
var splt = str.split("");
var rev = splt.reverse();
var once = ['Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'];
var twos = ['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
var tens = ['', 'Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety'];
numLength = rev.length;
var word = new Array();
var j = 0;
for (i = 0; i < numLength; i++) {
switch (i) {
case 0:
if ((rev[i] == 0) || (rev[i + 1] == 1)) {
word[j] = '';
}
else {
word[j] = '' + once[rev[i]];
}
word[j] = word[j];
break;
case 1:
aboveTens();
break;
case 2:
if (rev[i] == 0) {
word[j] = '';
}
else if ((rev[i - 1] == 0) || (rev[i - 2] == 0)) {
word[j] = once[rev[i]] + " Hundred ";
}
else {
word[j] = once[rev[i]] + " Hundred and";
}
break;
case 3:
if (rev[i] == 0 || rev[i + 1] == 1) {
word[j] = '';
}
else {
word[j] = once[rev[i]];
}
if ((rev[i + 1] != 0) || (rev[i] > 0)) {
word[j] = word[j] + " Thousand";
}
break;
case 4:
aboveTens();
break;
case 5:
if ((rev[i] == 0) || (rev[i + 1] == 1)) {
word[j] = '';
}
else {
word[j] = once[rev[i]];
}
if (rev[i + 1] !== '0' || rev[i] > '0') {
word[j] = word[j] + " Lakh";
}
break;
case 6:
aboveTens();
break;
case 7:
if ((rev[i] == 0) || (rev[i + 1] == 1)) {
word[j] = '';
}
else {
word[j] = once[rev[i]];
}
if (rev[i + 1] !== '0' || rev[i] > '0') {
word[j] = word[j] + " Crore";
}
break;
case 8:
aboveTens();
break;
// This is optional.
// case 9:
// if ((rev[i] == 0) || (rev[i + 1] == 1)) {
// word[j] = '';
// }
// else {
// word[j] = once[rev[i]];
// }
// if (rev[i + 1] !== '0' || rev[i] > '0') {
// word[j] = word[j] + " Arab";
// }
// break;
// case 10:
// aboveTens();
// break;
default: break;
}
j++;
}
function aboveTens() {
if (rev[i] == 0) { word[j] = ''; }
else if (rev[i] == 1) { word[j] = twos[rev[i - 1]]; }
else { word[j] = tens[rev[i]]; }
}
word.reverse();
var finalOutput = '';
for (i = 0; i < numLength; i++) {
finalOutput = finalOutput + word[i];
}
document.getElementById(outputControl).innerHTML = finalOutput;
}
</script>
</head>
<body>
<h1>
HTML - Convert numbers to words using JavaScript</h1>
<input id="Text1" type="text" onkeypress="return onlyNumbers(this.value);" onkeyup="NumToWord(this.value,'divDisplayWords');"
maxlength="9" style="background-color: #efefef; border: 2px solid #CCCCC; font-size: large" />
<br />
<br />
<div id="divDisplayWords" style="font-size: 13; color: Teal; font-family: Arial;">
</div>
</body>
</html>
回答by Ben E
I like the result I got here which i think is easy to read and short enough to fit as a solution.
我喜欢我在这里得到的结果,我认为它很容易阅读并且足够短,可以作为解决方案。
function NumInWords (number) {
const first = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
const tens = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
const mad = ['', 'thousand', 'million', 'billion', 'trillion'];
let word = '';
for (let i = 0; i < mad.length; i++) {
let tempNumber = number%(100*Math.pow(1000,i));
if (Math.floor(tempNumber/Math.pow(1000,i)) !== 0) {
if (Math.floor(tempNumber/Math.pow(1000,i)) < 20) {
word = first[Math.floor(tempNumber/Math.pow(1000,i))] + mad[i] + ' ' + word;
} else {
word = tens[Math.floor(tempNumber/(10*Math.pow(1000,i)))] + '-' + first[Math.floor(tempNumber/Math.pow(1000,i))%10] + mad[i] + ' ' + word;
}
}
tempNumber = number%(Math.pow(1000,i+1));
if (Math.floor(tempNumber/(100*Math.pow(1000,i))) !== 0) word = first[Math.floor(tempNumber/(100*Math.pow(1000,i)))] + 'hunderd ' + word;
}
return word;
}
console.log(NumInWords(89754697976431))
And the result is :
结果是:
eighty-nine trillion seven hundred fifty-four billion six hundred ninety-seven million nine hundred seventy-six thousand four hundred thirty-one
八十九万亿七百五十四亿六亿九千九千七百七万六千四百三十一
回答by Baji
Converting the input string into a number rather than keeping it as a string, limits the solution to the maximum allowed float / integer value on that machine/browser. My script below handles currency up to 1 Trillion dollars - 1 cent :-). I can be extended to handle up to 999 Trillions by adding 3 or 4 lines of code.
将输入字符串转换为数字而不是将其保留为字符串,将解决方案限制为该机器/浏览器上允许的最大浮点数/整数值。我下面的脚本处理的货币高达 1 万亿美元 - 1 美分 :-)。通过添加 3 或 4 行代码,I 可以扩展到处理多达 999 万亿。
var ones = ["","One","Two","Three","Four","Five","Six","Seven","Eight",
"Nine","Ten","Eleven","Twelve","Thirteen","Fourteen",
"Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"];
var tens = ["","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy",
"Eighty","Ninety"];
function words999(n999) { // n999 is an integer less than or equal to 999.
//
// Accept any 3 digit int incl 000 & 999 and return words.
//
var words = ''; var Hn = 0; var n99 = 0;
Hn = Math.floor(n999 / 100); // # of hundreds in it
if (Hn > 0) { // if at least one 100
words = words99(Hn) + " Hundred"; // one call for hundreds
}
n99 = n999 - (Hn * 100); // subtract the hundreds.
words += ((words == '')?'':' ') + words99(n99); // combine the hundreds with tens & ones.
return words;
} // function words999( n999 )
function words99(n99) { // n99 is an integer less than or equal to 99.
//
// Accept any 2 digit int incl 00 & 99 and return words.
//
var words = ''; var Dn = 0; var Un = 0;
Dn = Math.floor(n99 / 10); // # of tens
Un = n99 % 10; // units
if (Dn > 0 || Un > 0) {
if (Dn < 2) {
words += ones[Dn * 10 + Un]; // words for a # < 20
} else {
words += tens[Dn];
if (Un > 0) words += "-" + ones[Un];
}
} // if ( Dn > 0 || Un > 0 )
return words;
} // function words99( n99 )
function getAmtInWords(id1, id2) { // use numeric value of id1 to populate text in id2
//
// Read numeric amount field and convert into word amount
//
var t1 = document.getElementById(id1).value;
var t2 = t1.trim();
amtStr = t2.replace(/,/g,''); // 3,456,789.12 = 123456789.12
dotPos = amtStr.indexOf('.'); // position of dot before cents, -ve if it doesn't exist.
if (dotPos > 0) {
dollars = amtStr.slice(0,dotPos); // 1234.56 = 1234
cents = amtStr.slice(dotPos+1); // 1234.56 = .56
} else if (dotPos == 0) {
dollars = '0';
cents = amtStr.slice(dotPos+1); // 1234.56 = .56
} else {
dollars = amtStr.slice(0); // 1234 = 1234
cents = '0';
}
t1 = '000000000000' + dollars; // to extend to trillion, use 15 zeros
dollars = t1.slice(-12); // and -15 here.
billions = Number(dollars.substr(0,3));
millions = Number(dollars.substr(3,3));
thousands = Number(dollars.substr(6,3));
hundreds = Number(dollars.substr(9,3));
t1 = words999(billions); bW = t1.trim(); // Billions in words
t1 = words999(millions); mW = t1.trim(); // Millions in words
t1 = words999(thousands); tW = t1.trim(); // Thousands in words
t1 = words999(hundreds); hW = t1.trim(); // Hundreds in words
t1 = words99(cents); cW = t1.trim(); // Cents in words
var totAmt = '';
if (bW != '') totAmt += ((totAmt != '') ? ' ' : '') + bW + ' Billion';
if (mW != '') totAmt += ((totAmt != '') ? ' ' : '') + mW + ' Million';
if (tW != '') totAmt += ((totAmt != '') ? ' ' : '') + tW + ' Thousand';
if (hW != '') totAmt += ((totAmt != '') ? ' ' : '') + hW + ' Dollars';
if (cW != '') totAmt += ((totAmt != '') ? ' and ' : '') + cW + ' Cents';
// alert('totAmt = ' + totAmt); // display words in a alert
t1 = document.getElementById(id2).value;
t2 = t1.trim();
if (t2 == '') document.getElementById(id2).value = totAmt;
return false;
} // function getAmtInWords( id1, id2 )
// ======================== [ End Code ] ====================================
回答by eat_chocolate
This is in response to @LordZardeck's comment to @naomik's excellent answer above. Sorry, I would've commented directly but I've never posted before so I don't have the privilege to do so, so I am posting here instead.
这是对@LordZardeck 对上面@naomik 的出色回答的评论的回应。抱歉,我会直接发表评论,但我以前从未发表过,所以我没有这样做的特权,所以我在这里发帖。
Anyhow, I just happened to translate the ES5 version to a more readable form this past weekend so I'm sharing it here. This should be faithful to the original (including the recent edit) and I hope the naming is clear and accurate.
无论如何,我刚好在上周末将 ES5 版本翻译成更易读的形式,所以我在这里分享。这应该忠实于原始(包括最近的编辑),我希望命名清晰准确。
function int_to_words(int) {
if (int === 0) return 'zero';
var ONES = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'];
var TENS = ['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'];
var SCALE = ['','thousand','million','billion','trillion','quadrillion','quintillion','sextillion','septillion','octillion','nonillion'];
// Return string of first three digits, padded with zeros if needed
function get_first(str) {
return ('000' + str).substr(-3);
}
// Return string of digits with first three digits chopped off
function get_rest(str) {
return str.substr(0, str.length - 3);
}
// Return string of triplet convereted to words
function triplet_to_words(_3rd, _2nd, _1st) {
return (_3rd == '0' ? '' : ONES[_3rd] + ' hundred ') + (_1st == '0' ? TENS[_2nd] : TENS[_2nd] && TENS[_2nd] + '-' || '') + (ONES[_2nd + _1st] || ONES[_1st]);
}
// Add to words, triplet words with scale word
function add_to_words(words, triplet_words, scale_word) {
return triplet_words ? triplet_words + (scale_word && ' ' + scale_word || '') + ' ' + words : words;
}
function iter(words, i, first, rest) {
if (first == '000' && rest.length === 0) return words;
return iter(add_to_words(words, triplet_to_words(first[0], first[1], first[2]), SCALE[i]), ++i, get_first(rest), get_rest(rest));
}
return iter('', 0, get_first(String(int)), get_rest(String(int)));
}
回答by ifedayo israel
I modified MC Shaman's code to fix the bug of single number having and appear before it
我修改了MC Shaman的代码,修复了单号之前有和出现的bug
function numberToEnglish( n ) {
var string = n.toString(), units, tens, scales, start, end, chunks, chunksLen, chunk, ints, i, word, words, and = 'and';
/* Remove spaces and commas */
string = string.replace(/[, ]/g,"");
/* Is number zero? */
if( parseInt( string ) === 0 ) {
return 'zero';
}
/* Array of units as words */
units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ];
/* Array of tens as words */
tens = [ '', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ];
/* Array of scales as words */
scales = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quatttuor-decillion', 'quindecillion', 'sexdecillion', 'septen-decillion', 'octodecillion', 'novemdecillion', 'vigintillion', 'centillion' ];
/* Split user arguemnt into 3 digit chunks from right to left */
start = string.length;
chunks = [];
while( start > 0 ) {
end = start;
chunks.push( string.slice( ( start = Math.max( 0, start - 3 ) ), end ) );
}
/* Check if function has enough scale words to be able to stringify the user argument */
chunksLen = chunks.length;
if( chunksLen > scales.length ) {
return '';
}
/* Stringify each integer in each chunk */
words = [];
for( i = 0; i < chunksLen; i++ ) {
chunk = parseInt( chunks[i] );
if( chunk ) {
/* Split chunk into array of individual integers */
ints = chunks[i].split( '' ).reverse().map( parseFloat );
/* If tens integer is 1, i.e. 10, then add 10 to units integer */
if( ints[1] === 1 ) {
ints[0] += 10;
}
/* Add scale word if chunk is not zero and array item exists */
if( ( word = scales[i] ) ) {
words.push( word );
}
/* Add unit word if array item exists */
if( ( word = units[ ints[0] ] ) ) {
words.push( word );
}
/* Add tens word if array item exists */
if( ( word = tens[ ints[1] ] ) ) {
words.push( word );
}
/* Add 'and' string after units or tens integer if: */
if( ints[0] || ints[1] ) {
/* Chunk has a hundreds integer or chunk is the first of multiple chunks */
if( ints[2] || (i + 1) > chunksLen ) {
words.push( and );
}
}
/* Add hundreds word if array item exists */
if( ( word = units[ ints[2] ] ) ) {
words.push( word + ' hundred' );
}
}
}
return words.reverse().join( ' ' );
}
// - - - - - Tests - - - - - -
function figure(val) {
finalFig = numberToEnglish(val);
document.getElementById("words").innerHTML = finalFig;
}
<span id="words"></span>
<input id="number" type="text" onkeyup=figure(this.value) />
回答by Vilas Shetkar
var inWords = function(totalRent){
//console.log(totalRent);
var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
var number = parseFloat(totalRent).toFixed(2).split(".");
var num = parseInt(number[0]);
var digit = parseInt(number[1]);
//console.log(num);
if ((num.toString()).length > 9) return 'overflow';
var n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
var d = ('00' + digit).substr(-2).match(/^(\d{2})$/);;
if (!n) return; var str = '';
str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
str += (n[5] != 0) ? (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'Rupee ' : '';
str += (d[1] != 0) ? ((str != '' ) ? "and " : '') + (a[Number(d[1])] || b[d[1][0]] + ' ' + a[d[1][1]]) + 'Paise ' : 'Only!';
console.log(str);
return str;
}
This is modified code supports for Indian Rupee with 2 decimal place.
这是对印度卢比的修改代码支持,小数点后两位。


