security fix in master branch
[openemr.git] / library / classes / NumberToText.class.php
blob1fdb39e70216c6af9fac4783d9a83c55401ff47f
1 <?php
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");
20 /** Word for "cents"
21 * @package NumberToText */
22 define("N2T_CENTS", "cents");
23 /** Word for one "cent"
24 * @package NumberToText */
25 define("N2T_CENTS_ONE", "cent");
26 /** Word for "and"
27 * @package NumberToText */
28 define("N2T_AND", "and");
29 /** Word for "negative"
30 * @package NumberToText */
31 define("N2T_NEGATIVE", "negative");
33 class NumberToText
37 var $number;
38 var $currency;
39 var $capatalize;
40 var $and;
42 function __construct($number, $currency = false, $capatalize = false, $and = false)
44 $this->number = $number;
45 $this->currency = $currency;
46 $this->capatalize = $capatalize;
47 $this->and = $and;
50 /** Number to text converter. Converts a number into a textual description, such as
51 * "one hundred thousand and twenty-five".
53 * Now supports _any_ size number, and negative numbers. To pass numbers > 2 ^32, you must
54 * pass them as a string, as PHP only has 32-bit integers.
56 * @author Greg MacLelan
57 * @version 1.1
58 * @param int $number The number to convert
59 * @param bool $currency True to convert as a dollar amount
60 * @param bool $capatalize True to capatalize every word (except "and")
61 * @param bool $and True to use "and" (ie. "one hundred AND six")
62 * @return The textual description of the number, as a string.
63 * @package NumberToText
66 function convert()
68 $number = $this->number;
69 $currency = $this->currency;
70 $capatalize = $this->capatalize;
71 $and = $this->and;
73 $big = unserialize(N2T_BIG);
74 $small = unserialize(N2T_SMALL);
76 // get rid of leading 0's
78 while ($number{0} == 0) {
79 $number = substr($number,1);
83 if ($number === 0) {
84 return "zero";
87 $text = "";
89 //$negative = ($number < 0); // check for negative
90 //$number = abs($number); // make sure we have a +ve number
91 if (substr($number, 0, 1) == "-") {
92 $negative = true;
93 $number = substr($number, 1); // abs()
94 } else {
95 $negative = false;
98 // get the integer and decimal parts
99 //$int_o = $int = floor($number); // store into two vars
100 if ($pos = strpos($number, ".")) {
101 $int_o = $int = substr($number, 0, $pos);
102 $decimal_o = $decimal = substr($number, $pos + 1);
103 } else {
104 $int_o = $int = $number;
105 $decimal_o = $decimal = 0;
108 // $int_o and $decimal_o are for "original value"
109 // conversion for integer part:
111 $section = 0; // $section controls "thousand" "million" etc
112 $text = '';
113 do {
114 // keep breaking down into 3 digits ($convert) and the rest
115 //$convert = $int % 1000;
116 //$int = floor($int / 1000);
118 if ($section > count($big) - 1) {
119 // ran out of names for numbers this big, call recursively
120 $text = NumberToText($int, false, false, $and)." ".$big[$section-1]." ".$text;
121 $int = 0;
122 } else {
123 // we can handle it
125 if (strlen($int)<3) {
126 $convert = $int;
127 $int = 0;
128 } else {
129 $convert = substr($int, -3); // grab the last 3 digits
130 $int = substr($int, 0, -1 * strlen($convert));
133 if ($convert > 0) {
134 // we have something here, put it in
135 if ($section > 0) {
136 $text = $this->n2t_convertthree($convert, $and, ($int > 0))." ".$big[$section-1]." ".$text;
137 } else {
138 $text = $this->n2t_convertthree($convert, $and, ($int > 0));
143 $section++;
144 } while ($int > 0);
146 // conversion for decimal part:
148 if ($currency && floor($number)) {
149 // add " dollars"
150 $text .= " ".($int_o == 1 ? N2T_DOLLARS_ONE : N2T_DOLLARS)." ";
153 if ($decimal && $currency) {
154 // if we have any cents, add those
155 if ($int_o > 0) {
156 $text .= " ".N2T_AND." ";
159 $cents = substr($decimal, 0, 2); // (0.)2342 -> 23
160 $decimal = substr($decimal, 2); // (0.)2345.. -> 45..
162 $text .= $this->n2t_convertthree($cents, false, true); // explicitly show "and" if there was an $int
165 if ($decimal) {
166 // any remaining decimals (whether or not $currency is set)
167 $text .= " point";
168 for ($i = 0; $i < strlen($decimal); $i++) {
169 // go through one number at a time
170 $text .= " ".$small[$decimal{$i}];
175 if ($decimal_o && $currency) {
176 // add " cents" (if we're doing currency and had decimals)
177 $text .= " ".($decimal_o == 1 ? N2T_CENTS_ONE : N2T_CENTS);
180 // check for negative
181 if ($negative) {
182 $text = N2T_NEGATIVE." ".$text;
185 // capatalize words
186 if ($capatalize) {
187 // easier to capatalize all words then un-capatalize "and"
188 $text = str_replace(ucwords(N2T_AND), N2T_AND, ucwords($text));
191 return trim($text);
194 /** This is a utility function of n2t. It converts a 3-digit number
195 * into a textual description. Normally this is not called by itself.
197 * @param int $number The 3-digit number to convert (0 - 999)
198 * @param bool $and True to put the "and" in the string
199 * @param bool $preceding True if there are preceding members, puts an
200 * explicit and in (ie 1001 => one thousand AND one)
201 * @return The textual description of the number, as a string
202 * @package NumberToText
204 function n2t_convertthree($number, $and, $preceding)
206 $small = unserialize(N2T_SMALL);
207 $medium = unserialize(N2T_MEDIUM);
209 $text = "";
211 if ($hundreds = floor($number / 100)) {
212 // we have 100's place
213 $text .= $small[$hundreds]." hundred ";
216 $tens = $number % 100;
217 if ($tens) {
218 // we still have values
219 if ($and && ($hundreds || $preceding)) {
220 $text .= " ".N2T_AND." ";
223 if ($tens < 20) {
224 $text .= $small[$tens];
225 } else {
226 $text .= $medium[floor($tens/10)];
227 if ($ones = $tens % 10) {
228 $text .= "-".$small[$ones];
233 return $text;
236 function getmicrotime()
238 list($usec, $sec) = explode(" ", microtime());
239 return ((float)$usec + (float)$sec);