Name: verbose() - readable number.toString();
Author: senocular: www.senocular.com
Date: 1899-12-31T00:54:35.100
Documentation:
Number VERBOSE: translates a number into a readable string.
Returns:
- a worded 'verbose' string representation of the number
Note:
- this ignores any decimals
- any number 1 quadrillion or more resolves to "a lot of", see example 3 ;)
Application:
- 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
Update:
- fixed bug in not recognizing 10's
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
}