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=>'forty', 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 = false) {
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
110 // keep breaking down into 3 digits ($convert) and the rest
111 //$convert = $int % 1000;
112 //$int = floor($int / 1000);
114 if ($section > count($big) - 1) {
115 // ran out of names for numbers this big, call recursively
116 $text = NumberToText($int, false, false, $and)." ".$big[$section-1]." ".$text;
121 if (strlen($int)<3) {
125 $convert = substr($int, -3); // grab the last 3 digits
126 $int = substr($int, 0, -1 * strlen($convert));
130 // we have something here, put it in
131 if ( $section > 0 ) {
132 $text = $this->n2t_convertthree($convert, $and, ($int > 0))." ".$big[$section-1]." ".$text;
134 $text = $this->n2t_convertthree($convert, $and, ($int > 0));
142 // conversion for decimal part:
144 if ($currency && floor($number)) {
146 $text .= " ".($int_o == 1 ? N2T_DOLLARS_ONE
: N2T_DOLLARS
)." ";
149 if ($decimal && $currency) {
150 // if we have any cents, add those
152 $text .= " ".N2T_AND
." ";
155 $cents = substr($decimal,0,2); // (0.)2342 -> 23
156 $decimal = substr($decimal,2); // (0.)2345.. -> 45..
158 $text .= $this->n2t_convertthree($cents, false, true); // explicitly show "and" if there was an $int
162 // any remaining decimals (whether or not $currency is set)
164 for ($i = 0; $i < strlen($decimal); $i++
) {
165 // go through one number at a time
166 $text .= " ".$small[$decimal{$i}];
171 if ($decimal_o && $currency) {
172 // add " cents" (if we're doing currency and had decimals)
173 $text .= " ".($decimal_o == 1 ? N2T_CENTS_ONE
: N2T_CENTS
);
176 // check for negative
178 $text = N2T_NEGATIVE
." ".$text;
183 // easier to capatalize all words then un-capatalize "and"
184 $text = str_replace(ucwords(N2T_AND
), N2T_AND
, ucwords($text));
190 /** This is a utility function of n2t. It converts a 3-digit number
191 * into a textual description. Normally this is not called by itself.
193 * @param int $number The 3-digit number to convert (0 - 999)
194 * @param bool $and True to put the "and" in the string
195 * @param bool $preceding True if there are preceding members, puts an
196 * explicit and in (ie 1001 => one thousand AND one)
197 * @return The textual description of the number, as a string
198 * @package NumberToText
200 function n2t_convertthree($number, $and, $preceding) {
201 $small = unserialize(N2T_SMALL
);
202 $medium = unserialize(N2T_MEDIUM
);
206 if ($hundreds = floor($number / 100)) {
207 // we have 100's place
208 $text .= $small[$hundreds]." hundred ";
210 $tens = $number %
100;
212 // we still have values
213 if ($and && ($hundreds ||
$preceding)) {
214 $text .= " ".N2T_AND
." ";
218 $text .= $small[$tens];
220 $text .= $medium[floor($tens/10)];
221 if ($ones = $tens %
10) {
222 $text .= "-".$small[$ones];
230 function getmicrotime(){
231 list($usec, $sec) = explode(" ",microtime());
232 return ((float)$usec +
(float)$sec);