4umi.com/web/javascript/numbertoword

Number to word

Text Javascript

Use our convertor utility to convert numbers into their wordy counterparts, in other words: to make ‘22’ read like ‘twenty two’. Enter a number, such as 63 or 49770.2 and Javascript will write it out in words for you as you type. Only numeric characters are considered in the calculation. All other characters, and that includes the dot and the comma, are ignored.

The underlying javascript has been adapted from Yuk Cheng's post at comp.lang.javascript, which has been adapted from a posting by Hugh Bothwell to the php.general newsgroup. There is a limit to the precision of calculations. On tested machines, numbers 9999999999999900 and above produce random flaws. We are working on a fix and welcome all suggestions. Thank you for understanding.

The convertor

Input
Options

Output

The code

var ones = new Array(
 '',
 ' one',
 ' two',
 ' three',
 ' four',
 ' five',
 ' six',
 ' seven',
 ' eight',
 ' nine',
 ' ten',
 ' eleven',
 ' twelve',
 ' thirteen',
 ' fourteen',
 ' fifteen',
 ' sixteen',
 ' seventeen',
 ' eighteen',
 ' nineteen'
);

var tens = new Array(
 '',
 '',
 ' twenty',
 ' thirty',
 ' forty',
 ' fifty',
 ' sixty',
 ' seventy',
 ' eighty',
 ' ninety'
);

var triplets = new Array(
 '',
 ' thousand',
 ' million',
 ' billion',
 ' trillion',
 ' quadrillion',
 ' quintillion',
 ' sextillion',
 ' septillion',
 ' octillion',
 ' nonillion'
);

function convertTri(num, tri) {
 var str = '', comma = '',
  r = Math.round( ( num / 1000 ) - 0.5 ),
  x = Math.round( ( num / 100 ) - 0.5 ) % 10,
  y = Math.round( ( num % 100 ) - 0.5 );
 if(x > 0) { // hundreds
  str = ones[x] + ' hundred';
 }
 if(y < 20) { // ones and tens
  str += ones[y];
 } else {
  str += tens[Math.round( (y / 10) - 0.5 )] + ones[y % 10];
 }
 if(str) { // thousands
  str += triplets[tri];
 }
 if(r > 0) { // continue recursing?
  if(str) {
   var f = document.forms.f;
   comma += f.comma.checked ? ',' : '';
   comma += f.wrapx.checked ? '<br />' : '';
  }
  return convertTri( r, tri + 1 ) + comma + str;
 }
 return str;
}

function update() {
 var f = document.forms.f.elements, num = f.number.value, number, flip = 1, txt, t;
 if(!num) { return; }
 while( ( num.charAt()==' ' || num.charAt()=='0' ) && num.length>1 ) {
  num = num.substring(1);
  if( num.charAt()=='-' ) { flip *= -1; num = num.substring(1); }
 }
 num = num.replace( /[^0-9]/g, '' );
 number = parseInt( num, 10 );
 t = number ? ( ( flip<0 ? 'minus' : '' ) + convertTri( number, 0 ) + (f.comma.checked ? '.' : '') ) :  'zero';
 txt = t ? ( t.length + ' character' + ( t.length==1 ? '' : 's' ) + ' long:<br />' + t.big().bold() ) : 'not a number.';
 document.getElementById('word').innerHTML =
  '<a href="#' + number + '" onclick="ran(this.hash);return false;">' +
  String( t ? number : f.number.value ) + '</a> is ' + txt + '<br /><br />' +
 document.getElementById('word').innerHTML;
 //f.number.focus();
}

function eva(o) {
 document.forms.f.elements.number.value =
  eval( document.forms.f.elements.number.value + o.value );
 update();
}

function ran(n) {
 document.forms.f.elements.number.value =
  ( n || new String( Math.random() ).substring( Math.ceil( Math.random() * 16 ) + 1 ) ).replace( /[^-\d]/g, '' );
 update();
}

function upfont() {
 var f = document.forms.f.elements;
 gid('word').className = f.fontx.checked ? 'serif' : 'sans';
 f.number.focus();
}

ran( window.location.hash || '' );

Reference