
| Path : /home/gujo45me/public_html/limalab/pdf/letras/ |
| Disable Functions : exec,passthru,shell_exec,system System : Linux server-604606.appsiete.com 3.10.0-1160.119.1.el7.tuxcare.els25.x86_64 #1 SMP Wed Oct 1 17:37:27 UTC 2025 x86_64 [ Home ][ Zone-h ][ Jumping ][ Symlink ][ Mass Depes ][ Command ] |
| Current File : /home/gujo45me/public_html/limalab/pdf/letras/NumeroALetras.php |
<?php
/**
* Clase que implementa un coversor de números
* a letras.
*
* Soporte para PHP >= 5.4
* Para soportar PHP 5.3, declare los arreglos
* con la función array.
*
* @author AxiaCore S.A.S
*
*/
class NumeroALetras
{
private static $UNIDADES = [
'',
'UN ',
'DOS ',
'TRES ',
'CUATRO ',
'CINCO ',
'SEIS ',
'SIETE ',
'OCHO ',
'NUEVE ',
'DIEZ ',
'ONCE ',
'DOCE ',
'TRECE ',
'CATORCE ',
'QUINCE ',
'DIECISEIS ',
'DIECISIETE ',
'DIECIOCHO ',
'DIECINUEVE ',
'VEINTE '
];
private static $DECENAS = [
'VENTI',
'TREINTA ',
'CUARENTA ',
'CINCUENTA ',
'SESENTA ',
'SETENTA ',
'OCHENTA ',
'NOVENTA ',
'CIEN '
];
private static $CENTENAS = [
'CIENTO ',
'DOSCIENTOS ',
'TRESCIENTOS ',
'CUATROCIENTOS ',
'QUINIENTOS ',
'SEISCIENTOS ',
'SETECIENTOS ',
'OCHOCIENTOS ',
'NOVECIENTOS '
];
public static function convertir($number, $moneda = '', $centimos = '', $forzarCentimos = false)
{
$converted = '';
$decimales = '';
if (($number < 0) || ($number > 999999999)) {
return 'No es posible convertir el numero a letras';
}
$div_decimales = explode('.',$number);
if(count($div_decimales) > 1){
$number = $div_decimales[0];
$decNumberStr = (string) $div_decimales[1];
if(strlen($decNumberStr) == 2){
$decNumberStrFill = str_pad($decNumberStr, 9, '0', STR_PAD_LEFT);
$decCientos = substr($decNumberStrFill, 6);
$decimales = self::convertGroup($decCientos);
}
}
else if (count($div_decimales) == 1 && $forzarCentimos){
$decimales = 'CERO ';
}
$numberStr = (string) $number;
$numberStrFill = str_pad($numberStr, 9, '0', STR_PAD_LEFT);
$millones = substr($numberStrFill, 0, 3);
$miles = substr($numberStrFill, 3, 3);
$cientos = substr($numberStrFill, 6);
if (intval($millones) > 0) {
if ($millones == '001') {
$converted .= 'UN MILLON ';
} else if (intval($millones) > 0) {
$converted .= sprintf('%sMILLONES ', self::convertGroup($millones));
}
}
if (intval($miles) > 0) {
if ($miles == '001') {
$converted .= 'MIL ';
} else if (intval($miles) > 0) {
$converted .= sprintf('%sMIL ', self::convertGroup($miles));
}
}
if (intval($cientos) > 0) {
if ($cientos == '001') {
$converted .= 'UN ';
} else if (intval($cientos) > 0) {
$converted .= sprintf('%s ', self::convertGroup($cientos));
}
}
if(empty($decimales)){
//$valor_convertido = $converted . strtoupper($moneda);
$valor_convertido = $converted . 'CON 00/100';
} else {
//$valor_convertido = $converted . strtoupper($moneda) . ' CON ' . $decimales . ' ' . strtoupper($centimos);
$valor_convertido = $converted . 'CON ' . $div_decimales[1] . '/100';
}
return $valor_convertido;
}
private static function convertGroup($n)
{
$output = '';
if ($n == '100') {
$output = "CIEN ";
} else if ($n[0] !== '0') {
$output = self::$CENTENAS[$n[0] - 1];
}
$k = intval(substr($n,1));
if ($k <= 20) {
$output .= self::$UNIDADES[$k];
} else {
if(($k > 30) && ($n[2] !== '0')) {
$output .= sprintf('%sY %s', self::$DECENAS[intval($n[1]) - 2], self::$UNIDADES[intval($n[2])]);
} else {
$output .= sprintf('%s%s', self::$DECENAS[intval($n[1]) - 2], self::$UNIDADES[intval($n[2])]);
}
}
return $output;
}
}