3 //pulled from www.djgeespot.com/php, reworked as a class
5 /** Serialized Array of big names, thousand, million, etc
6 * @package NumberToText */
7 define("N2T_BIG", serialize(array('thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion')));
8 /** Serialized Array of medium names, twenty, thirty, etc
9 * @package NumberToText */
10 define("N2T_MEDIUM", serialize(array(2=>'twenty', 3=>'thirty', 4=>'fourty', 5=>'fifty', 6=>'sixty', 7=>'seventy', 8=>'eighty', 9=>'ninety')));
11 /** Serialized Array of small names, zero, one, etc.. up to eighteen, nineteen
12 * @package NumberToText */
13 define("N2T_SMALL", serialize(array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen')));
14 /** Word for "dollars"
15 * @package NumberToText */
16 define("N2T_DOLLARS", "dollars");
17 /** Word for one "dollar"
18 * @package NumberToText */
19 define("N2T_DOLLARS_ONE", "dollar");
21 * @package NumberToText */
22 define("N2T_CENTS", "cents");
23 /** Word for one "cent"
24 * @package NumberToText */
25 define("N2T_CENTS_ONE", "cent");
27 * @package NumberToText */
28 define("N2T_AND", "and");
29 /** Word for "negative"
30 * @package NumberToText */
31 define("N2T_NEGATIVE", "negative");
41 function NumberToText($number, $currency = false, $capatalize = false, $and = true) {
42 $this->number
= $number;
43 $this->currency
= $currency;
44 $this->capatalize
= $capatalize;
48 /** Number to text converter. Converts a number into a textual description, such as
49 * "one hundred thousand and twenty-five".
51 * Now supports _any_ size number, and negative numbers. To pass numbers > 2 ^32, you must
52 * pass them as a string, as PHP only has 32-bit integers.
54 * @author Greg MacLelan
56 * @param int $number The number to convert
57 * @param bool $currency True to convert as a dollar amount
58 * @param bool $capatalize True to capatalize every word (except "and")
59 * @param bool $and True to use "and" (ie. "one hundred AND six")
60 * @return The textual description of the number, as a string.
61 * @package NumberToText
65 $number = $this->number
;
66 $currency = $this->currency
;
67 $capatalize = $this->capatalize
;
70 $big = unserialize(N2T_BIG
);
71 $small = unserialize(N2T_SMALL
);
73 // get rid of leading 0's
75 while ($number{0} == 0) {
76 $number = substr($number,1);
86 //$negative = ($number < 0); // check for negative
87 //$number = abs($number); // make sure we have a +ve number
88 if (substr($number, 0, 1) == "-") {
90 $number = substr($number,1); // abs()
95 // get the integer and decimal parts
96 //$int_o = $int = floor($number); // store into two vars
97 if ($pos = strpos($number,".")) {
98 $int_o = $int = substr($number,0,$pos);
99 $decimal_o = $decimal = substr($number,$pos +
1);
101 $int_o = $int = $number;
102 $decimal_o = $decimal = 0;
104 // $int_o and $decimal_o are for "original value"
105 // conversion for integer part:
107 $section = 0; // $section controls "thousand" "million" etc
109 // keep breaking down into 3 digits ($convert) and the rest
110 //$convert = $int % 1000;
111 //$int = floor($int / 1000);
113 if ($section > count($big) - 1) {
114 // ran out of names for numbers this big, call recursively
115 $text = NumberToText($int, false, false, $and)." ".$big[$section-1]." ".$text;
120 $convert = substr($int, -3); // grab the last 3 digits
121 $int = substr($int, 0, -1 * strlen($convert));
124 // we have something here, put it in
125 $text = $this->n2t_convertthree($convert, $and, ($int > 0))." ".$big[$section-1]." ".$text;
133 // conversion for decimal part:
135 if ($currency && floor($number)) {
137 $text .= " ".($int_o == 1 ? N2T_DOLLARS_ONE
: N2T_DOLLARS
)." ";
140 if ($decimal && $currency) {
141 // if we have any cents, add those
143 $text .= " ".N2T_AND
." ";
146 $cents = substr($decimal,0,2); // (0.)2342 -> 23
147 $decimal = substr($decimal,2); // (0.)2345.. -> 45..
149 $text .= $this->n2t_convertthree($cents, false, true); // explicitly show "and" if there was an $int
153 // any remaining decimals (whether or not $currency is set)
155 for ($i = 0; $i < strlen($decimal); $i++
) {
156 // go through one number at a time
157 $text .= " ".$small[$decimal{$i}];
162 if ($decimal_o && $currency) {
163 // add " cents" (if we're doing currency and had decimals)
164 $text .= " ".($decimal_o == 1 ? N2T_CENTS_ONE
: N2T_CENTS
);
167 // check for negative
169 $text = N2T_NEGATIVE
." ".$text;
174 // easier to capatalize all words then un-capatalize "and"
175 $text = str_replace(ucwords(N2T_AND
), N2T_AND
, ucwords($text));
181 /** This is a utility function of n2t. It converts a 3-digit number
182 * into a textual description. Normally this is not called by itself.
184 * @param int $number The 3-digit number to convert (0 - 999)
185 * @param bool $and True to put the "and" in the string
186 * @param bool $preceding True if there are preceding members, puts an
187 * explicit and in (ie 1001 => one thousand AND one)
188 * @return The textual description of the number, as a string
189 * @package NumberToText
191 function n2t_convertthree($number, $and, $preceding) {
192 $small = unserialize(N2T_SMALL
);
193 $medium = unserialize(N2T_MEDIUM
);
197 if ($hundreds = floor($number / 100)) {
198 // we have 100's place
199 $text .= $small[$hundreds]." hundred ";
201 $tens = $number %
100;
203 // we still have values
204 if ($and && ($hundreds ||
$preceding)) {
205 $text .= " ".N2T_AND
." ";
209 $text .= $small[$tens];
211 $text .= $medium[floor($tens/10)];
212 if ($ones = $tens %
10) {
213 $text .= "-".$small[$ones];
221 function getmicrotime(){
222 list($usec, $sec) = explode(" ",microtime());
223 return ((float)$usec +
(float)$sec);