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