Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've modified the posting from Šime Vidas - <a href="http://jsfiddle.net/j5kdG/" rel="nofollow">http://jsfiddle.net/j5kdG/</a> To include dollars, cents, commas and "and" in the appropriate places. There's an optional ending if it requires "zero cents" or no mention of cents if 0.</p> <p>This function structure did my head in a bit but I learned heaps. Thanks Sime.</p> <p>Someone might find a better way of processing this.</p> <p>Code:</p> <pre><code>var str=''; var str2=''; var str3 =[]; function convertNum(inp,end){ str2=''; str3 = []; var NUMBER2TEXT = { 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'], sep: ['', ' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion', ' sextillion'] }; (function( ones, tens, sep ) { var vals = inp.split("."),val,pos,postsep=' '; for (p in vals){ val = vals[p], arr = [], str = '', i = 0; if ( val.length === 0 ) {return 'No value';} val = parseInt( (p==1 &amp;&amp; val.length===1 )?val*10:val, 10 ); if ( isNaN( val ) || p&gt;=2) {return 'Invalid value'; } while ( val ) { arr.push( val % 1000 ); val = parseInt( val / 1000, 10 ); } pos = arr.length; function trimx (strx) { return strx.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); } function seps(sepi,i){ var s = str3.length if (str3[s-1][0]){ if (str3[s-2][1] === str3[s-1][0]){ str = str.replace(str3[s-2][1],'') } } var temp = str.split(sep[i-2]); if (temp.length &gt; 1){ if (trimx(temp[0]) ==='' &amp;&amp; temp[1].length &gt; 1 ){ str = temp[1]; } } return sepi + str ; } while ( arr.length ) { str = (function( a ) { var x = Math.floor( a / 100 ), y = Math.floor( a / 10 ) % 10, z = a % 10; postsep = (arr.length != 0)?', ' : ' ' ; if ((x+y+z) === 0){ postsep = ' ' }else{ if (arr.length == pos-1 &amp;&amp; x===0 &amp;&amp; pos &gt; 1 ){ postsep = ' and ' } } str3.push([trimx(str)+"",trimx(sep[i])+""]); return (postsep)+( x &gt; 0 ? ones[x] + ' hundred ' + (( x == 0 &amp;&amp; y &gt;= 0 || z &gt;0 )?' and ':' ') : ' ' ) + ( y &gt;= 2 ? tens[y] + ((z===0)?' ':'-') + ones[z] : ones[10*y + z] ); })( arr.shift() ) +seps( sep[i++] ,i ) ; } if (p==0){ str2 += str + ' dollars'} if (p==1 &amp;&amp; !end){str2 += (str!='')?' and '+ str + ' cents':'' } if (p==1 &amp;&amp; end ){str2 += ' and ' + ((str==='')?'zero':str) + ' cents '} } })( NUMBER2TEXT.ones , NUMBER2TEXT.tens , NUMBER2TEXT.sep ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload