dolibarr 20.0.2
functionsnumtoword.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2015 Víctor Ortiz Pérez <victor@accett.com.mx>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
37function dol_convertToWord($num, $langs, $currency = '', $centimes = false)
38{
39 //$num = str_replace(array(',', ' '), '', trim($num)); This should be useless since $num MUST be a php numeric value
40 if (!$num) {
41 return false;
42 }
43
44 if ($centimes && strlen((string) $num) == 1) {
45 $num = $num * 10;
46 }
47
48 if (isModEnabled('numberwords')) {
49 $concatWords = $langs->getLabelFromNumber($num, $currency);
50 return $concatWords;
51 } else {
52 $TNum = explode('.', (string) $num);
53
54 $num = (int) $TNum[0];
55 $words = array();
56 $list1 = array(
57 '',
58 $langs->transnoentitiesnoconv('one'),
59 $langs->transnoentitiesnoconv('two'),
60 $langs->transnoentitiesnoconv('three'),
61 $langs->transnoentitiesnoconv('four'),
62 $langs->transnoentitiesnoconv('five'),
63 $langs->transnoentitiesnoconv('six'),
64 $langs->transnoentitiesnoconv('seven'),
65 $langs->transnoentitiesnoconv('eight'),
66 $langs->transnoentitiesnoconv('nine'),
67 $langs->transnoentitiesnoconv('ten'),
68 $langs->transnoentitiesnoconv('eleven'),
69 $langs->transnoentitiesnoconv('twelve'),
70 $langs->transnoentitiesnoconv('thirteen'),
71 $langs->transnoentitiesnoconv('fourteen'),
72 $langs->transnoentitiesnoconv('fifteen'),
73 $langs->transnoentitiesnoconv('sixteen'),
74 $langs->transnoentitiesnoconv('seventeen'),
75 $langs->transnoentitiesnoconv('eighteen'),
76 $langs->transnoentitiesnoconv('nineteen')
77 );
78 $list2 = array(
79 '',
80 $langs->transnoentitiesnoconv('ten'),
81 $langs->transnoentitiesnoconv('twenty'),
82 $langs->transnoentitiesnoconv('thirty'),
83 $langs->transnoentitiesnoconv('forty'),
84 $langs->transnoentitiesnoconv('fifty'),
85 $langs->transnoentitiesnoconv('sixty'),
86 $langs->transnoentitiesnoconv('seventy'),
87 $langs->transnoentitiesnoconv('eighty'),
88 $langs->transnoentitiesnoconv('ninety'),
89 $langs->transnoentitiesnoconv('hundred')
90 );
91 $list3 = array(
92 '',
93 $langs->transnoentitiesnoconv('thousand'),
94 $langs->transnoentitiesnoconv('million'),
95 $langs->transnoentitiesnoconv('billion'),
96 $langs->transnoentitiesnoconv('trillion'),
97 $langs->transnoentitiesnoconv('quadrillion')
98 );
99
100 $num_length = strlen((string) $num);
101 $levels = (int) (($num_length + 2) / 3);
102 $max_length = $levels * 3;
103 $num = substr('00'.$num, -$max_length);
104 $num_levels = str_split($num, 3);
105 $nboflevels = count($num_levels);
106 for ($i = 0; $i < $nboflevels; $i++) {
107 $levels--;
108 $hundreds = (int) ($num_levels[$i] / 100);
109 $hundreds = ($hundreds ? ' '.$list1[$hundreds].' '.$langs->transnoentities('hundred').($hundreds == 1 ? '' : 's').' ' : '');
110 $tens = (int) ($num_levels[$i] % 100);
111 $singles = '';
112 if ($tens < 20) {
113 $tens = ($tens ? ' '.$list1[$tens].' ' : '');
114 } else {
115 $tens = (int) ($tens / 10);
116 $tens = ' '.$list2[$tens].' ';
117 $singles = (int) ($num_levels[$i] % 10);
118 $singles = ' '.$list1[$singles].' ';
119 }
120 $words[] = $hundreds.$tens.$singles.(($levels && (int) ($num_levels[$i])) ? ' '.$list3[$levels].' ' : '');
121 } //end for loop
122 $commas = count($words);
123 if ($commas > 1) {
124 $commas = $commas - 1;
125 }
126 $concatWords = implode(' ', $words);
127 // Delete multi whitespaces
128 $concatWords = trim(preg_replace('/[ ]+/', ' ', $concatWords));
129
130 if (!empty($currency)) {
131 $concatWords .= ' '.$currency;
132 }
133
134 // If we need to write cents call again this function for cents
135 $decimalpart = empty($TNum[1]) ? '' : preg_replace('/0+$/', '', $TNum[1]);
136
137 if ($decimalpart) {
138 if (!empty($currency)) {
139 $concatWords .= ' '.$langs->transnoentities('and');
140 }
141
142 $concatWords .= ' '.dol_convertToWord($decimalpart, $langs, '', true);
143 if (!empty($currency)) {
144 $concatWords .= ' '.$langs->transnoentities('centimes');
145 }
146 }
147 return $concatWords;
148 }
149}
150
151
161function dolNumberToWord($numero, $langs, $numorcurrency = 'number')
162{
163 // If the number is negative convert to positive and return -1 if it is too long
164 if ($numero < 0) {
165 $numero *= -1;
166 }
167 if ($numero >= 1000000000001) {
168 return -1;
169 }
170
171 // Get 2 decimals to cents, another functions round or truncate
172 $strnumber = number_format($numero, 10);
173 $len = strlen($strnumber);
174 for ($i = 0; $i < $len; $i++) {
175 if ($strnumber[$i] == '.') {
176 $parte_decimal = $strnumber[$i + 1].$strnumber[$i + 2];
177 break;
178 }
179 }
180
181 /*In dolibarr 3.6.2 (my current version) doesn't have $langs->default and
182 in case exist why ask $lang like a parameter?*/
183 if (((is_object($langs) && $langs->getDefaultLang(0) == 'es_MX') || (!is_object($langs) && $langs == 'es_MX')) && $numorcurrency == 'currency') {
184 if ($numero >= 1 && $numero < 2) {
185 return ("UN PESO ".$parte_decimal." / 100 M.N.");
186 } elseif ($numero >= 0 && $numero < 1) {
187 return ("CERO PESOS ".$parte_decimal." / 100 M.N.");
188 } elseif ($numero >= 1000000 && $numero < 1000001) {
189 return ("UN MILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
190 } elseif ($numero >= 1000000000000 && $numero < 1000000000001) {
191 return ("UN BILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
192 } else {
193 $entexto = "";
194 $number = $numero;
195 if ($number >= 1000000000) {
196 $CdMMillon = (int) ($numero / 100000000000);
197 $numero = $numero - $CdMMillon * 100000000000;
198 $DdMMillon = (int) ($numero / 10000000000);
199 $numero = $numero - $DdMMillon * 10000000000;
200 $UdMMillon = (int) ($numero / 1000000000);
201 $numero = $numero - $UdMMillon * 1000000000;
202 $entexto .= hundreds2text($CdMMillon, $DdMMillon, $UdMMillon);
203 $entexto .= " MIL ";
204 }
205 if ($number >= 1000000) {
206 $CdMILLON = (int) ($numero / 100000000);
207 $numero = $numero - $CdMILLON * 100000000;
208 $DdMILLON = (int) ($numero / 10000000);
209 $numero = $numero - $DdMILLON * 10000000;
210 $udMILLON = (int) ($numero / 1000000);
211 $numero = $numero - $udMILLON * 1000000;
212 $entexto .= hundreds2text($CdMILLON, $DdMILLON, $udMILLON);
213 if (!$CdMMillon && !$DdMMillon && !$UdMMillon && !$CdMILLON && !$DdMILLON && $udMILLON == 1) {
214 $entexto .= " MILL&OacuteN ";
215 } else {
216 $entexto .= " MILLONES ";
217 }
218 }
219 if ($number >= 1000) {
220 $cdm = (int) ($numero / 100000);
221 $numero = $numero - $cdm * 100000;
222 $ddm = (int) ($numero / 10000);
223 $numero = $numero - $ddm * 10000;
224 $udm = (int) ($numero / 1000);
225 $numero = $numero - $udm * 1000;
226 $entexto .= hundreds2text($cdm, $ddm, $udm);
227 if ($cdm || $ddm || $udm) {
228 $entexto .= " MIL ";
229 }
230 }
231 $c = (int) ($numero / 100);
232 $numero = $numero - $c * 100;
233 $d = (int) ($numero / 10);
234 $u = (int) $numero - $d * 10;
235 $entexto .= hundreds2text($c, $d, $u);
236 if (!$cdm && !$ddm && !$udm && !$c && !$d && !$u && $number > 1000000) {
237 $entexto .= " DE";
238 }
239 $entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
240 }
241 return $entexto;
242 }
243 return -1;
244}
245
254function hundreds2text($hundreds, $tens, $units)
255{
256 if ($hundreds == 1 && $tens == 0 && $units == 0) {
257 return "CIEN";
258 }
259 $centenas = array("CIENTO", "DOSCIENTOS", "TRESCIENTOS", "CUATROCIENTOS", "QUINIENTOS", "SEISCIENTOS", "SETECIENTOS", "OCHOCIENTOS", "NOVECIENTOS");
260 $decenas = array("", "", "TREINTA ", "CUARENTA ", "CINCUENTA ", "SESENTA ", "SETENTA ", "OCHENTA ", "NOVENTA ");
261 $veintis = array("VEINTE", "VEINTIUN", "VEINTID&OacuteS", "VEINTITR&EacuteS", "VEINTICUATRO", "VEINTICINCO", "VEINTIS&EacuteIS", "VEINTISIETE", "VEINTIOCHO", "VEINTINUEVE");
262 $diecis = array("DIEZ", "ONCE", "DOCE", "TRECE", "CATORCE", "QUINCE", "DIECIS&EacuteIS", "DIECISIETE", "DIECIOCHO", "DIECINUEVE");
263 $unidades = array("UN", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE", "OCHO", "NUEVE");
264 $entexto = "";
265 if ($hundreds != 0) {
266 $entexto .= $centenas[$hundreds - 1];
267 }
268 if ($tens > 2) {
269 if ($hundreds != 0) {
270 $entexto .= " ";
271 }
272 $entexto .= $decenas[$tens - 1];
273 if ($units != 0) {
274 $entexto .= " Y ";
275 $entexto .= $unidades[$units - 1];
276 }
277 return $entexto;
278 } elseif ($tens == 2) {
279 if ($hundreds != 0) {
280 $entexto .= " ";
281 }
282 $entexto .= " ".$veintis[$units];
283 return $entexto;
284 } elseif ($tens == 1) {
285 if ($hundreds != 0) {
286 $entexto .= " ";
287 }
288 $entexto .= $diecis[$units];
289 return $entexto;
290 }
291 if ($units != 0) {
292 if ($hundreds != 0 || $tens != 0) {
293 $entexto .= " ";
294 }
295 $entexto .= $unidades[$units - 1];
296 }
297 return $entexto;
298}
dol_convertToWord($num, $langs, $currency='', $centimes=false)
Function to return a number into a text.
hundreds2text($hundreds, $tens, $units)
hundreds2text
dolNumberToWord($numero, $langs, $numorcurrency='number')
Function to return number or amount in text.