P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Number
:
verbose() - readable number.toString();
author:
senocular
[+]
,
Submitted: 12.08.02 5a
• Last Edit: 03.10.03 7a
Number.prototype.verbose = function(){ if (!this) return "zero"; var n = "00"+Math.floor(this).toString(); if (n.indexOf("e") != -1) return "a lot of"; var a=["","one","two","three","four","five","six","seven","eight","nine"]; var b=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]; var c=["","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"]; var d=[""," thousand"," million"," billion"," trillion"]; var s,k1,k2,k3,v="",l=Math.floor(n.length/3); while (l--){ s = n.substr((-3*(l+1)),3); if (v.length && s) v += ", "; if (k1=number(s.charAt(0))) v += a[k1] +" hundred"; k3=number(s.charAt(2)); if (k2=number(s.charAt(1))){ if (k1) v += " and "; else if (v.length) v += "and "; if (k2 == 1) v += b[k3]; else{ v += c[k2]; if (k3){ if (k2) v += "-"; v += a[k3]; } } }else if (k3){ if (k1) v += " and "; else if (!l && v.length) v += "and "; v += a[k3]; } if (k1 || k2 || k3) v += d[l]; } return v; } /***************** EXAMPLE *****************\ num = 4 trace(num.verbose()); // four money = 9463008014; trace("I have "+ money.verbose() +" dollars"); // I have nine billion, four hundred and sixty-three million, eight thousand, and fourteen dollars // (must be nice) cats = 750273456435810948; trace("Aunt sally has "+ cats.verbose() +" cats"); // Aunt sally has a lot of cats \*******************************************/
usage
[big]Number [b]VERBOSE[/b]: translates a number into a readable string[/big]. [b]Returns[/b]: - a worded 'verbose' string representation of the number [b]Note[/b]: - this ignores any decimals - any number 1 quadrillion or more resolves to "a lot of", see example 3 ;) [b]Application[/b]: - need to fill out any checks? - can also be nice for debugging when dealing with large numbers and you find it difficult to tell right off exactly how big your number is [b]Update[/b]: - fixed bug in not recognizing 10's
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@