Here is an excerpt of my script for Great Britain. Its cut the number in triplets and use the replace method with function not to replace but to define the word string... Its give a short but compact script.
function triplets(n){var c='',m=0;
if (100<=n) {m=Math.floor(n/100);c+=triplets(m)+' hundred';n-=m*100}
n.toString().replace(/^[2-9](?=\d)/,function(a){
c+=(m?' and ':' ')+('twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety'.split(',')[a-2]);n-=(+a)*10});
if (n==0) return c;else if (c && n<10) c+='-';
return c+' '+'one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen'.split(',')[n-1];
}
function anyNumber(n){var lng,isS,chn='';
wds=',thousand,million,thousand million'.split(',');
n.toString().replace(/(\d{1,3})(?=((\d\d\d)*)$)/g,function(a,b,c){
lng=c.toString().length/3;
isS=triplets(m=a.replace(/^0+/,''));
if (isS) chn+=((!lng) && m<100 && /thousand$/.test(chn)?' and ':' ')+isS.replace(/- /g,'-')+' '+wds[lng];
});
return chn.replace(/\s+/g,' ');
}
[COLOR="#0000FF"]// Use [/COLOR]
alert(anyNumber(897563))
The script is made for positive numbers (0 give an empty string) less than 1,000,000,000,000 which should be enough by choosing well units...
For USA replace the thousand million by billion and remove the word and.
Unless I'm mistaken ?