Skip to content

Instantly share code, notes, and snippets.

@shinigamicorei7
Created January 15, 2016 20:52
Show Gist options
  • Save shinigamicorei7/a5c73dfcad49721c469e to your computer and use it in GitHub Desktop.
Save shinigamicorei7/a5c73dfcad49721c469e to your computer and use it in GitHub Desktop.
Pequeña función que transforma números a letras con PHP
<?php
function numeros_a_letras($numero)
{
$numero = sprintf("%0.2f", $numero);
$valor = "";
$enteros = array("CERO", "UNO", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE", "OCHO", "NUEVE", "DIEZ");
$decimales1 = array("DIEZ", "ONCE", "DOCE", "TRECE", "CATORCE", "QUINCE", "DIECISÉIS", "DIECISIETE", "DIECIOCHO", "DIECINUEVE");
$decimales2 = array("", "UNO", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE", "OCHO", "NUEVE");
$decimales3 = array("", "", "VEINTE", "TREINTA", "CUARENTA", "CINCUENTA", "SESENTA", "SETENTA", "OCHENTA", "NOVENTA");
list($entero, $decimales) = explode(".", $numero);
if ($entero >= 0 && $entero <= 10)
{
$valor = $enteros[$entero];
}
else
{
echo "Error, fuera de rango: $entero";
die();
}
if ($decimales > 0)
{
$valor .= " COMA ";
$decimales = strval(100 + $decimales);
$datos = str_split($decimales);
$posicion = intval($datos[2]);
$posicion1 = intval($datos[1]);
if ($datos[1] == 0)
{
$valor .= $enteros[0] . " " . $enteros[$datos[2]];
}
elseif ($datos[1] == 1)
{
$valor .= $decimales1[$posicion];
}
else
{
$valor .= $decimales3[$posicion1];
if ($datos[2] == 0)
{
$valor .= "";
}
else
{
$valor .= " Y " . $decimales2[$posicion];
}
}
}
else
{
$valor .= ' COMA CERO';
}
return $valor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment