我正在制作一个代码,将给定的数量转换为单词,继续谷歌是我在谷歌搜索后得到的.但我觉得它有点冗长的代码来完成一个简单的任务.两个正则表达式和两个for
循环,我想要更简单的东西.
我想尽量缩短它.并将发布我想出的
有什么建议?
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,' ');
}
Run Code Online (Sandbox Code Playgroud)
另外,上面的代码转换为英文编号系统,如百万/亿,我不是南亚编号系统.比如Lakhs和Crores
Sal*_*man 103
这是一个更短的代码.一个RegEx,没有循环.在南亚编号系统中,您可以根据需要进行转换
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);
};
Run Code Online (Sandbox Code Playgroud)
<span id="words"></span>
<input id="number" type="text" />
Run Code Online (Sandbox Code Playgroud)
唯一的限制是,你可以转换最多9位数,我认为在大多数情况下绰绰有余..
更新:看起来这比我想象的更有用.我刚刚在npm发布了这个.https://www.npmjs.com/package/num-words
Tha*_*you 29
" 看似简单的任务." - Potatoswatter
确实.在这个问题的细节中有许多小恶魔.解决这个问题非常有趣.
编辑:此更新采用更多的组合方法.以前有一个很大的功能包含了其他一些专有功能.相反,这次我们定义了可用于多种任务的通用可重用函数.在我们看了看之后更多关于那些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(' ');
};
Run Code Online (Sandbox Code Playgroud)
以下是依赖项:
您会注意到这些要求旁边没有文档,因为他们的意图立即明确.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))];
Run Code Online (Sandbox Code Playgroud)
"所以这些让它变得更好?"
看看代码是如何清理的
// 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)));
Run Code Online (Sandbox Code Playgroud)
最重要的是,我们在新代码中添加的实用程序功能可以在您的应用程序中的其他位置使用.这意味着,作为numToWords
以这种方式实现的副作用,我们可以免费获得其他功能.红利苏打!
一些测试
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
Run Code Online (Sandbox Code Playgroud)
Runnable演示
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
Run Code Online (Sandbox Code Playgroud)
如果要查看ES5变体,可以使用babel.js来转换代码
McS*_*man 14
我花了一些时间为此开发出更好的解决方案.它可以处理非常大的数字但是一旦它们超过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");
Run Code Online (Sandbox Code Playgroud)
您可能想尝试递归.它适用于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): "");
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我喜欢我在这里得到的结果,我认为它很容易阅读并且足够短,可以作为解决方案。
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))
Run Code Online (Sandbox Code Playgroud)
结果是:
八十九万亿七百五十四亿六亿九千七千七万六千四百三十一
<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>
Run Code Online (Sandbox Code Playgroud)
小智 5
我修改了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 argument 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;
}
Run Code Online (Sandbox Code Playgroud)
<span id="words"></span>
<input id="number" type="text" onkeyup=figure(this.value) />
Run Code Online (Sandbox Code Playgroud)
虽然这个问题是在 8 年前提出的,有各种解决方案和答案,但最简单的解决方案可以轻松更新以增加规模,只需在数组中插入一个名称而无需修改代码;下面的函数也使用了非常短的代码。
该解决方案适用于数字的英语阅读(不是南亚系统),并使用标准的美国英语(美国方式)书写大数字。即它不使用英国系统(英国系统使用“和”一词,例如:“三十二万二千”)。
提供了测试用例以及输入字段。
请记住,我们将“无符号整数”转换为单词。
您可以增加scale[] array
超过六亿。
由于该函数很短,因此您可以将其用作内部函数,例如在使用十进制数的货币转换中,并对整数部分和小数部分调用两次。
希望它有用。
/********************************************************
* @function : integerToWords()
* @purpose : Converts Unsigned Integers to Words
* Using String Triplet Array.
* @version : 1.05
* @author : Mohsen Alyafei
* @date : 15 January 2021
* @param : {number} [integer numeric or string]
* @returns : {string} The wordified number string
********************************************************/
const Ones = ["","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"],
Tens = ["","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety","Hundred"],
Scale = ["","Thousand","Million","Billion","Trillion","Quadrillion","Quintillion","Sextillion"];
//==================================
const integerToWords = (n = 0) => {
if (n == 0) return "Zero"; // check for zero
n = ("0".repeat(2*(n+="").length % 3) + n).match(/.{3}/g); // create triplets array
if (n.length > Scale.length) return "Too Large"; // check if larger than scale array
let out= ""; return n.forEach((Triplet,pos) => { // loop into array for each triplet
if (+Triplet) { out+=' ' +(+Triplet[0] ? Ones[+Triplet[0]]+' '+ Tens[10] : "") +
' ' + (+Triplet.substr(1)< 20 ? Ones[+Triplet.substr(1)] :
Tens[+Triplet[1]] + (+Triplet[2] ? "-" : "") + Ones[+Triplet[2]]) +
' ' + Scale[n.length-pos-1]; }
}),out.replace(/\s+/g,' ').trim();}; // lazy job using trim()
//==================================
//=========================================
// Test Cases
//=========================================
var r=0; // test tracker
r |= test(0,"Zero");
r |= test(5,"Five");
r |= test(10,"Ten");
r |= test(19,"Nineteen");
r |= test(33,"Thirty-Three");
r |= test(100,"One Hundred");
r |= test(111,"One Hundred Eleven");
r |= test(890,"Eight Hundred Ninety");
r |= test(1234,"One Thousand Two Hundred Thirty-Four");
r |= test(12345,"Twelve Thousand Three Hundred Forty-Five");
r |= test(123456,"One Hundred Twenty-Three Thousand Four Hundred Fifty-Six");
r |= test(1234567,"One Million Two Hundred Thirty-Four Thousand Five Hundred Sixty-Seven");
r |= test(12345678,"Twelve Million Three Hundred Forty-Five Thousand Six Hundred Seventy-Eight");
r |= test(123456789,"One Hundred Twenty-Three Million Four Hundred Fifty-Six Thousand Seven Hundred Eighty-Nine");
r |= test(1234567890,"One Billion Two Hundred Thirty-Four Million Five Hundred Sixty-Seven Thousand Eight Hundred Ninety");
r |= test(1001,"One Thousand One");
r |= test(10001,"Ten Thousand One");
r |= test(100001,"One Hundred Thousand One");
r |= test(1000001,"One Million One");
r |= test(10000001,"Ten Million One");
r |= test(100000001,"One Hundred Million One");
r |= test(12012,"Twelve Thousand Twelve");
r |= test(120012,"One Hundred Twenty Thousand Twelve");
r |= test(1200012,"One Million Two Hundred Thousand Twelve");
r |= test(12000012,"Twelve Million Twelve");
r |= test(120000012,"One Hundred Twenty Million Twelve");
r |= test(75075,"Seventy-Five Thousand Seventy-Five");
r |= test(750075,"Seven Hundred Fifty Thousand Seventy-Five");
r |= test(7500075,"Seven Million Five Hundred Thousand Seventy-Five");
r |= test(75000075,"Seventy-Five Million Seventy-Five");
r |= test(750000075,"Seven Hundred Fifty Million Seventy-Five");
r |= test(1000,"One Thousand");
r |= test(1000000,"One Million");
r |= test(1000000000,"One Billion");
r |= test(1000000000000,"One Trillion");
r |= test("1000000000000000","One Quadrillion");
r |= test("1000000000000000000","One Quintillion");
r |= test("1000000100100100100","One Quintillion One Hundred Billion One Hundred Million One Hundred Thousand One Hundred");
if (r==0) console.log("All Tests Passed.");
//=====================================
// Tester Function
//=====================================
function test(n,should) {
let result = integerToWords(n);
if (result !== should) {console.log(`${n} Output : ${result}\n${n} Should be: ${should}`);return 1;}
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" name="number" placeholder="Please enter an Integer Number" onkeyup="word.innerHTML=integerToWords(this.value)" />
<div id="word"></div>
Run Code Online (Sandbox Code Playgroud)
class N2WHindi {\n private static readonly zeroTo99: string[] =\n \'|\xe0\xa4\x8f\xe0\xa4\x95|\xe0\xa4\xa6\xe0\xa5\x8b|\xe0\xa4\xa4\xe0\xa5\x80\xe0\xa4\xa8|\xe0\xa4\x9a\xe0\xa4\xbe\xe0\xa4\xb0|\xe0\xa4\xaa\xe0\xa4\xbe\xe0\xa4\x81\xe0\xa4\x9a|\xe0\xa4\x9b\xe0\xa4\x83|\xe0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0\xa5\x8d\xe0\xa4\xa0\xe0\xa4\xbe\xe0\xa4\xa8\xe0\xa4\xac\xe0\xa5\x87|\xe0\xa4\xa8\xe0\xa4\xbf\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xaf\xe0\xa4\xbe\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xac\xe0\xa5\x87\'.split(\n \'|\'\n );\n\n private static readonly place: string[] = \'\xe0\xa4\xb9\xe0\xa4\x9c\xe0\xa4\xbc\xe0\xa4\xbe\xe0\xa4\xb0|\xe0\xa4\xb2\xe0\xa4\xbe\xe0\xa4\x96|\xe0\xa4\x95\xe0\xa4\xb0\xe0\xa5\x8b\xe0\xa4\xa1\xe0\xa4\xbc|\xe0\xa4\x85\xe0\xa4\xb0\xe0\xa4\xac|\xe0\xa4\x96\xe0\xa4\xb0\xe0\xa4\xac|\xe0\xa4\xa8\xe0\xa5\x80\xe0\xa4\xb2\'.split(\'|\');\n\n public static convert(x: string): string {\n let n: number = x.length;\n x = n === 0 ? \'00\' : n === 1 || n % 2 === 0 ? \'0\' + x : x;\n n = x.length;\n let r: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528];\n if (n > 0) {\n const v: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 1)) - 48];\n if (v) r = v + \' \xe0\xa4\xb8\xe0\xa5\x8c\' + (r ? \' \' + r : \'\');\n }\n for (let i = 0; n > 0; i++) {\n const v: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528];\n if (v) r = v + \' \' + N2WHindi.place[i] + (r ? \' \' + r : \'\');\n }\n return r || \'\xe0\xa4\xb6\xe0\xa5\x82\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xaf\';\n }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\nclass N2WIndian {\n private static readonly zeroTo99: string[] = [];\n private static readonly place: string[] = \'Thousand|Lakh|Crore|Arab|Kharab|Nil\'.split(\'|\');\n\n static {\n const ones: string[] =\n \'|One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven|Twelve|Thirteen|Fourteen|Fifteen|Sixteen|Seventeen|Eighteen|Nineteen\'.split(\n \'|\'\n );\n\n const tens: string[] = \'||Twenty|Thirty|Forty|Fifty|Sixty|Seventy|Eighty|Ninety\'.split(\'|\');\n\n for (let i = 0; i < 100; i++) {\n const t: number = Math.floor(i / 10);\n const o: number = i % 10;\n N2WIndian.zeroTo99.push(t < 2 ? ones[i] : tens[t] + (o ? \' \' + ones[o] : \'\'));\n }\n }\n\n public static convert(x: string): string {\n let n: number = x.length;\n x = n === 0 ? \'00\' : n === 1 || n % 2 === 0 ? \'0\' + x : x;\n n = x.length;\n let r = N2WIndian.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528];\n if (n >= 1) {\n const v: string = N2WIndian.zeroTo99[x.charCodeAt((n -= 1)) - 48];\n if (v) r = v + \' Hundred\' + (r ? \' \' + r : \'\');\n }\n for (let i = 0; n > 0; i++) {\n const v: string = N2WIndian.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528];\n if (v) r = v + \' \' + N2WIndian.place[i] + (r ? \' \' + r : \'\');\n }\n return r || \'Zero\';\n }\n}\n\n\n
Run Code Online (Sandbox Code Playgroud)\nclass N2WIntl {\n private static readonly zeroTo999: string[] = [];\n\n private static readonly place =\n \'|Thousand|Million|Billion|Trillion|Quadrillion|Quintillion|Sextillion|Septillion|Octillion|Nonillion|Decillion|Undecillion|Duodecillion|Tredecillion|Quattuordecillion|Quindecillion|Sedecillion|Septendecillion|Octodecillion|Novendecillion|Vigintillion|Unvigintillion|Duovigintillion|Tresvigintillion|Quattuorvigintillion|Quinvigintillion|Sesvigintillion|Septemvigintillion|Octovigintillion|Novemvigintillion|Trigintillion|Untrigintillion|Duotrigintillion|Trestrigintillion|Quattuortrigintillion|Quintrigintillion|Sestrigintillion|Septentrigintillion|Octotrigintillion|Noventrigintillion|Quadragintillion\'.split(\n \'|\'\n );\n\n static {\n const ones =\n \'|One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven|Twelve|Thirteen|Fourteen|Fifteen|Sixteen|Seventeen|Eighteen|Nineteen\'.split(\n \'|\'\n );\n const tens = \'||Twenty|Thirty|Forty|Fifty|Sixty|Seventy|Eighty|Ninety\'.split(\'|\');\n for (let i = 0; i < 100; i++) {\n const t = Math.floor(i / 10);\n const o = i % 10;\n N2WIntl.zeroTo999[i] = t < 2 ? ones[i] : tens[t] + (o === 0 ? \'\' : \' \' + ones[o]);\n }\n for (let i = 100; i < 1000; i++) {\n const h = Math.floor(i / 100);\n const t = Math.floor(i / 10) % 10;\n const o = i % 10;\n const r = N2WIntl.zeroTo999[h] + \' Hundred\';\n N2WIntl.zeroTo999[i] = t === 0 && o === 0 ? r : r + \' \' + N2WIntl.zeroTo999[t * 10 + o];\n }\n }\n\n public static convert(x: string): string {\n let n = x.length;\n x = n === 0 ? \'000\' : (n % 3 === 1 ? \'00\' : n % 3 === 2 ? \'0\' : \'\') + x;\n n = x.length;\n let r = \'\';\n for (let i = 0; n > 0; i++) {\n const v: string =\n N2WIntl.zeroTo999[\n x.charCodeAt((n -= 3)) * 100 + x.charCodeAt(n + 1) * 10 + x.charCodeAt(n + 2) - 5328\n ];\n if (v) r = v + (i ? \' \' + N2WIntl.place[i] : \'\') + (r ? \' \' + r : \'\');\n }\n return r || \'Zero\';\n }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\nconst test = () => {\n {\n let n = 5000000;\n const test: string = \'1234567890\';\n const t0 = performance.now();\n while (n-- > 0) {\n N2WHindi.convert(test);\n }\n const t1 = performance.now();\n console.log(\'1234567890 to 5 Million times: \' + (t1 - t0) + \'ms\');\n console.log(\'10^15 -1 :\\n\' + \'9\'.repeat(15) + \'\\n\' + N2WHindi.convert(\'9\'.repeat(15)));\n }\n\n {\n let n = 5000000;\n const test: string = \'1234567890\';\n const t0 = performance.now();\n while (n-- > 0) {\n N2WIndian.convert(test);\n }\n const t1 = performance.now();\n console.log(\'1234567890 to 5 Million times: \' + (t1 - t0) + \'ms\');\n console.log(\'10^15 -1 :\\n\' + \'9\'.repeat(15) + \'\\n\' + N2WIndian.convert(\'9\'.repeat(15)));\n }\n\n {\n let n = 5000000;\n const test: string = \'1234567890\';\n const t0 = performance.now();\n while (n-- > 0) {\n N2WIntl.convert(test);\n }\n const t1 = performance.now();\n console.log(\'1234567890 to 5 Million times: \' + (t1 - t0) + \'ms\');\n console.log(\'10^126 -1 :\\n\' + \'9\'.repeat(126) + \'\\n\' + N2WIntl.convert(\'9\'.repeat(126)));\n }\n};\n\ntest();\n
Run Code Online (Sandbox Code Playgroud)\nclass N2WHindi {\n private static readonly zeroTo99: string[] =\n \'|\xe0\xa4\x8f\xe0\xa4\x95|\xe0\xa4\xa6\xe0\xa5\x8b|\xe0\xa4\xa4\xe0\xa5\x80\xe0\xa4\xa8|\xe0\xa4\x9a\xe0\xa4\xbe\xe0\xa4\xb0|\xe0\xa4\xaa\xe0\xa4\xbe\xe0\xa4\x81\xe0\xa4\x9a|\xe0\xa4\x9b\xe0\xa4\x83|\xe0\xa4\xb8\xe0\xa4\xbe\xe0\xa4\xa4|\xe0\xa4\x86\xe0\xa4\xa0|\xe0\xa4\xa8\xe0\xa5\x8c|\xe0\xa4\xa6\xe0\xa4\xb6|\xe0\xa4\x97\xe0\xa5\x8d\xe0\xa4\xaf\xe0\xa4\xbe\xe0\xa4\xb0\xe0\xa4\xb9|\xe0\xa4\xac\xe0\xa4\xbe\xe0\xa4\xb0\xe0\xa4\xb9|\xe0\xa4\xa4\xe0\xa5\x87\xe0\xa4\xb0\xe0\xa4\xb9|\xe0\xa4\x9a\xe0\xa5\x8c\xe0\xa4\xa6\xe0\xa4\xb9|\xe0\xa4\xaa\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xa6\xe0\xa5\x8d\xe0\xa4\xb0\xe0\x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\xa4\xac|\xe0\xa4\x96\xe0\xa4\xb0\xe0\xa4\xac|\xe0\xa4\xa8\xe0\xa5\x80\xe0\xa4\xb2\'.split(\'|\');\n\n public static convert(x: string): string {\n let n: number = x.length;\n x = n === 0 ? \'00\' : n === 1 || n % 2 === 0 ? \'0\' + x : x;\n n = x.length;\n let r: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528];\n if (n > 0) {\n const v: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 1)) - 48];\n if (v) r = v + \' \xe0\xa4\xb8\xe0\xa5\x8c\' + (r ? \' \' + r : \'\');\n }\n for (let i = 0; n > 0; i++) {\n const v: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528];\n if (v) r = v + \' \' + N2WHindi.place[i] + (r ? \' \' + r : \'\');\n }\n return r || \'\xe0\xa4\xb6\xe0\xa5\x82\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xaf\';\n }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\nconst [r, p] = "23.54".split(\'.\');\n\n`${N2WHindi.convert(r)} \xe0\xa4\xb0\xe0\xa5\x81\xe0\xa4\xaa\xe0\xa4\xaf\xe0\xa4\xbe \xe0\xa4\x94\xe0\xa4\xb0 ${N2WHindi.convert(p)} \xe0\xa4\xaa\xe0\xa5\x88\xe0\xa4\xb8\xe0\xa4\xbe`\n\'\xe0\xa4\xa4\xe0\xa5\x87\xe0\xa4\x88\xe0\xa4\xb8 \xe0\xa4\xb0\xe0\xa5\x81\xe0\xa4\xaa\xe0\xa4\xaf\xe0\xa4\xbe \xe0\xa4\x94\xe0\xa4\xb0 \xe0\xa4\x9a\xe0\xa5\x8c\xe0\xa4\xb5\xe0\xa4\xa8 \xe0\xa4\xaa\xe0\xa5\x88\xe0\xa4\xb8\xe0\xa4\xbe\'\n\n`${N2WIndian.convert(r)} Rupees and ${N2WIndian.convert(p)} Paisa`\n\'Twenty Three Rupees and Fifty Four Paisa\'\n\n`${N2WIntl.convert(r)} Dollars and ${N2WIntl.convert(p)} Cents`\n\'Twenty Three Dollars and Fifty Four Cents\'\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
106404 次 |
最近记录: |