Fix number to string
This commit is contained in:
commit
fbcf95ba02
@ -37,10 +37,10 @@ function dol_convertToWord($num, $langs, $currency = false, $centimes = false)
|
|||||||
{
|
{
|
||||||
global $conf;
|
global $conf;
|
||||||
|
|
||||||
//$num = str_replace(array(',', ' '), '', trim($num)); This should be useless since $num MUST be a php numeric value
|
//$num = str_replace(array(',', ' '), '', trim($num)); This should be useless since $num MUST be a php numeric value
|
||||||
if (!$num) {
|
if (!$num) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($centimes && strlen($num) == 1) {
|
if ($centimes && strlen($num) == 1) {
|
||||||
$num = $num * 10;
|
$num = $num * 10;
|
||||||
@ -57,78 +57,79 @@ function dol_convertToWord($num, $langs, $currency = false, $centimes = false)
|
|||||||
return $concatWords;
|
return $concatWords;
|
||||||
} else {
|
} else {
|
||||||
$TNum = explode('.', $num);
|
$TNum = explode('.', $num);
|
||||||
$num = (int) $TNum[0];
|
|
||||||
$words = array();
|
|
||||||
$list1 = array(
|
|
||||||
'',
|
|
||||||
$langs->transnoentitiesnoconv('one'),
|
|
||||||
$langs->transnoentitiesnoconv('two'),
|
|
||||||
$langs->transnoentitiesnoconv('three'),
|
|
||||||
$langs->transnoentitiesnoconv('four'),
|
|
||||||
$langs->transnoentitiesnoconv('five'),
|
|
||||||
$langs->transnoentitiesnoconv('six'),
|
|
||||||
$langs->transnoentitiesnoconv('seven'),
|
|
||||||
$langs->transnoentitiesnoconv('eight'),
|
|
||||||
$langs->transnoentitiesnoconv('nine'),
|
|
||||||
$langs->transnoentitiesnoconv('ten'),
|
|
||||||
$langs->transnoentitiesnoconv('eleven'),
|
|
||||||
$langs->transnoentitiesnoconv('twelve'),
|
|
||||||
$langs->transnoentitiesnoconv('thirteen'),
|
|
||||||
$langs->transnoentitiesnoconv('fourteen'),
|
|
||||||
$langs->transnoentitiesnoconv('fifteen'),
|
|
||||||
$langs->transnoentitiesnoconv('sixteen'),
|
|
||||||
$langs->transnoentitiesnoconv('seventeen'),
|
|
||||||
$langs->transnoentitiesnoconv('eighteen'),
|
|
||||||
$langs->transnoentitiesnoconv('nineteen')
|
|
||||||
);
|
|
||||||
$list2 = array(
|
|
||||||
'',
|
|
||||||
$langs->transnoentitiesnoconv('ten'),
|
|
||||||
$langs->transnoentitiesnoconv('twenty'),
|
|
||||||
$langs->transnoentitiesnoconv('thirty'),
|
|
||||||
$langs->transnoentitiesnoconv('forty'),
|
|
||||||
$langs->transnoentitiesnoconv('fifty'),
|
|
||||||
$langs->transnoentitiesnoconv('sixty'),
|
|
||||||
$langs->transnoentitiesnoconv('seventy'),
|
|
||||||
$langs->transnoentitiesnoconv('eighty'),
|
|
||||||
$langs->transnoentitiesnoconv('ninety'),
|
|
||||||
$langs->transnoentitiesnoconv('hundred')
|
|
||||||
);
|
|
||||||
$list3 = array(
|
|
||||||
'',
|
|
||||||
$langs->transnoentitiesnoconv('thousand'),
|
|
||||||
$langs->transnoentitiesnoconv('million'),
|
|
||||||
$langs->transnoentitiesnoconv('billion'),
|
|
||||||
$langs->transnoentitiesnoconv('trillion'),
|
|
||||||
$langs->transnoentitiesnoconv('quadrillion')
|
|
||||||
);
|
|
||||||
|
|
||||||
$num_length = strlen($num);
|
$num = (int) $TNum[0];
|
||||||
$levels = (int) (($num_length + 2) / 3);
|
$words = array();
|
||||||
$max_length = $levels * 3;
|
$list1 = array(
|
||||||
$num = substr('00'.$num, -$max_length);
|
'',
|
||||||
$num_levels = str_split($num, 3);
|
$langs->transnoentitiesnoconv('one'),
|
||||||
$nboflevels = count($num_levels);
|
$langs->transnoentitiesnoconv('two'),
|
||||||
for ($i = 0; $i < $nboflevels; $i++) {
|
$langs->transnoentitiesnoconv('three'),
|
||||||
$levels--;
|
$langs->transnoentitiesnoconv('four'),
|
||||||
$hundreds = (int) ($num_levels[$i] / 100);
|
$langs->transnoentitiesnoconv('five'),
|
||||||
$hundreds = ($hundreds ? ' '.$list1[$hundreds].' '.$langs->transnoentities('hundred').($hundreds == 1 ? '' : 's').' ' : '');
|
$langs->transnoentitiesnoconv('six'),
|
||||||
$tens = (int) ($num_levels[$i] % 100);
|
$langs->transnoentitiesnoconv('seven'),
|
||||||
$singles = '';
|
$langs->transnoentitiesnoconv('eight'),
|
||||||
if ($tens < 20) {
|
$langs->transnoentitiesnoconv('nine'),
|
||||||
$tens = ($tens ? ' '.$list1[$tens].' ' : '');
|
$langs->transnoentitiesnoconv('ten'),
|
||||||
} else {
|
$langs->transnoentitiesnoconv('eleven'),
|
||||||
$tens = (int) ($tens / 10);
|
$langs->transnoentitiesnoconv('twelve'),
|
||||||
$tens = ' '.$list2[$tens].' ';
|
$langs->transnoentitiesnoconv('thirteen'),
|
||||||
$singles = (int) ($num_levels[$i] % 10);
|
$langs->transnoentitiesnoconv('fourteen'),
|
||||||
$singles = ' '.$list1[$singles].' ';
|
$langs->transnoentitiesnoconv('fifteen'),
|
||||||
}
|
$langs->transnoentitiesnoconv('sixteen'),
|
||||||
$words[] = $hundreds.$tens.$singles.(($levels && (int) ($num_levels[$i])) ? ' '.$list3[$levels].' ' : '');
|
$langs->transnoentitiesnoconv('seventeen'),
|
||||||
} //end for loop
|
$langs->transnoentitiesnoconv('eighteen'),
|
||||||
$commas = count($words);
|
$langs->transnoentitiesnoconv('nineteen')
|
||||||
if ($commas > 1) {
|
);
|
||||||
$commas = $commas - 1;
|
$list2 = array(
|
||||||
}
|
'',
|
||||||
|
$langs->transnoentitiesnoconv('ten'),
|
||||||
|
$langs->transnoentitiesnoconv('twenty'),
|
||||||
|
$langs->transnoentitiesnoconv('thirty'),
|
||||||
|
$langs->transnoentitiesnoconv('forty'),
|
||||||
|
$langs->transnoentitiesnoconv('fifty'),
|
||||||
|
$langs->transnoentitiesnoconv('sixty'),
|
||||||
|
$langs->transnoentitiesnoconv('seventy'),
|
||||||
|
$langs->transnoentitiesnoconv('eighty'),
|
||||||
|
$langs->transnoentitiesnoconv('ninety'),
|
||||||
|
$langs->transnoentitiesnoconv('hundred')
|
||||||
|
);
|
||||||
|
$list3 = array(
|
||||||
|
'',
|
||||||
|
$langs->transnoentitiesnoconv('thousand'),
|
||||||
|
$langs->transnoentitiesnoconv('million'),
|
||||||
|
$langs->transnoentitiesnoconv('billion'),
|
||||||
|
$langs->transnoentitiesnoconv('trillion'),
|
||||||
|
$langs->transnoentitiesnoconv('quadrillion')
|
||||||
|
);
|
||||||
|
|
||||||
|
$num_length = strlen($num);
|
||||||
|
$levels = (int) (($num_length + 2) / 3);
|
||||||
|
$max_length = $levels * 3;
|
||||||
|
$num = substr('00'.$num, -$max_length);
|
||||||
|
$num_levels = str_split($num, 3);
|
||||||
|
$nboflevels = count($num_levels);
|
||||||
|
for ($i = 0; $i < $nboflevels; $i++) {
|
||||||
|
$levels--;
|
||||||
|
$hundreds = (int) ($num_levels[$i] / 100);
|
||||||
|
$hundreds = ($hundreds ? ' '.$list1[$hundreds].' '.$langs->transnoentities('hundred').($hundreds == 1 ? '' : 's').' ' : '');
|
||||||
|
$tens = (int) ($num_levels[$i] % 100);
|
||||||
|
$singles = '';
|
||||||
|
if ($tens < 20) {
|
||||||
|
$tens = ($tens ? ' '.$list1[$tens].' ' : '');
|
||||||
|
} else {
|
||||||
|
$tens = (int) ($tens / 10);
|
||||||
|
$tens = ' '.$list2[$tens].' ';
|
||||||
|
$singles = (int) ($num_levels[$i] % 10);
|
||||||
|
$singles = ' '.$list1[$singles].' ';
|
||||||
|
}
|
||||||
|
$words[] = $hundreds.$tens.$singles.(($levels && (int) ($num_levels[$i])) ? ' '.$list3[$levels].' ' : '');
|
||||||
|
} //end for loop
|
||||||
|
$commas = count($words);
|
||||||
|
if ($commas > 1) {
|
||||||
|
$commas = $commas - 1;
|
||||||
|
}
|
||||||
$concatWords = implode(' ', $words);
|
$concatWords = implode(' ', $words);
|
||||||
// Delete multi whitespaces
|
// Delete multi whitespaces
|
||||||
$concatWords = trim(preg_replace('/[ ]+/', ' ', $concatWords));
|
$concatWords = trim(preg_replace('/[ ]+/', ' ', $concatWords));
|
||||||
@ -138,16 +139,16 @@ function dol_convertToWord($num, $langs, $currency = false, $centimes = false)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we need to write cents call again this function for cents
|
// If we need to write cents call again this function for cents
|
||||||
if (!empty($TNum[1])) {
|
$decimalpart = $TNum[1];
|
||||||
if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('and');
|
$decimalpart = preg_replace('/0+$/', '', $decimalpart);
|
||||||
|
|
||||||
$decimalpart = $TNum[1];
|
if ($decimalpart) {
|
||||||
$decimalpart = preg_replace('/0+$/', '', $decimalpart);
|
if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('and');
|
||||||
|
|
||||||
$concatWords .= ' '.dol_convertToWord($decimalpart, $langs, '', true);
|
$concatWords .= ' '.dol_convertToWord($decimalpart, $langs, '', true);
|
||||||
if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('centimes');
|
if (!empty($currency)) $concatWords .= ' '.$langs->transnoentities('centimes');
|
||||||
}
|
}
|
||||||
return $concatWords;
|
return $concatWords;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,6 +168,7 @@ function dolNumberToWord($numero, $langs, $numorcurrency = 'number')
|
|||||||
if ($numero < 0) $numero *= -1;
|
if ($numero < 0) $numero *= -1;
|
||||||
if ($numero >= 1000000000001)
|
if ($numero >= 1000000000001)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// Get 2 decimals to cents, another functions round or truncate
|
// Get 2 decimals to cents, another functions round or truncate
|
||||||
$strnumber = number_format($numero, 10);
|
$strnumber = number_format($numero, 10);
|
||||||
$len = strlen($strnumber);
|
$len = strlen($strnumber);
|
||||||
@ -220,27 +222,27 @@ function dolNumberToWord($numero, $langs, $numorcurrency = 'number')
|
|||||||
else
|
else
|
||||||
$entexto .= " MILLONES ";
|
$entexto .= " MILLONES ";
|
||||||
}
|
}
|
||||||
if ($number >= 1000) {
|
if ($number >= 1000) {
|
||||||
$cdm = (int) ($numero / 100000);
|
$cdm = (int) ($numero / 100000);
|
||||||
$numero = $numero - $cdm * 100000;
|
$numero = $numero - $cdm * 100000;
|
||||||
$ddm = (int) ($numero / 10000);
|
$ddm = (int) ($numero / 10000);
|
||||||
$numero = $numero - $ddm * 10000;
|
$numero = $numero - $ddm * 10000;
|
||||||
$udm = (int) ($numero / 1000);
|
$udm = (int) ($numero / 1000);
|
||||||
$numero = $numero - $udm * 1000;
|
$numero = $numero - $udm * 1000;
|
||||||
$entexto .= hundreds2text($cdm, $ddm, $udm);
|
$entexto .= hundreds2text($cdm, $ddm, $udm);
|
||||||
if ($cdm || $ddm || $udm)
|
if ($cdm || $ddm || $udm)
|
||||||
$entexto .= " MIL ";
|
$entexto .= " MIL ";
|
||||||
|
}
|
||||||
|
$c = (int) ($numero / 100);
|
||||||
|
$numero = $numero - $c * 100;
|
||||||
|
$d = (int) ($numero / 10);
|
||||||
|
$u = (int) $numero - $d * 10;
|
||||||
|
$entexto .= hundreds2text($c, $d, $u);
|
||||||
|
if (!$cdm && !$ddm && !$udm && !$c && !$d && !$u && $number > 1000000)
|
||||||
|
$entexto .= " DE";
|
||||||
|
$entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
|
||||||
}
|
}
|
||||||
$c = (int) ($numero / 100);
|
return $entexto;
|
||||||
$numero = $numero - $c * 100;
|
|
||||||
$d = (int) ($numero / 10);
|
|
||||||
$u = (int) $numero - $d * 10;
|
|
||||||
$entexto .= hundreds2text($c, $d, $u);
|
|
||||||
if (!$cdm && !$ddm && !$udm && !$c && !$d && !$u && $number > 1000000)
|
|
||||||
$entexto .= " DE";
|
|
||||||
$entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
|
|
||||||
}
|
|
||||||
return $entexto;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user