Last active
August 29, 2015 14:03
-
-
Save MikeVelazcoMtz/8c725758816a511a7cec to your computer and use it in GitHub Desktop.
Este gist permite convertir cadenas numericas a texto. Es una adaptacion al metodo de PHP mostrado en http://php-aplicado.blogspot.mx/2013/05/convertir-numeros-letras-con-codeigniter-2-usando-ajax-jquery.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function my_str_pad($string){ | |
$string = $string.toString(); | |
len = $string.length; | |
if(len > 18) | |
return $string; | |
else | |
{ | |
$string = $string.split(""); | |
while($string.length < 18) | |
$string.unshift(" "); | |
return $string.join(""); | |
} | |
} | |
function subfijo($cifras){ | |
$cifras = $cifras.toString().trim(); | |
$strlen = $cifras.length; | |
$_sub = ''; | |
if( 4 <= $strlen && 6 >= $strlen){ | |
$_sub = 'MIL'; | |
} | |
return $_sub; | |
} | |
function my_abs(int){ | |
if(!isNaN(int)){ | |
int = parseFloat(int, 10); | |
int = (int < 0) ? (int *-1) :int; | |
return int; | |
} | |
} | |
function substr(str, start, len) { | |
// discuss at: http://phpjs.org/functions/substr/ | |
// version: 909.322 | |
// original by: Martijn Wieringa | |
// bugfixed by: T.Wild | |
// improved by: Onno Marsman | |
// improved by: Brett Zamir (http://brett-zamir.me) | |
// revised by: Theriault | |
// note: Handles rare Unicode characters if 'unicode.semantics' ini (PHP6) is set to 'on' | |
var i = 0, | |
allBMP = true, | |
es = 0, | |
el = 0, | |
se = 0, | |
ret = ''; | |
str += ''; | |
var end = str.length; | |
// BEGIN REDUNDANT | |
this.php_js = this.php_js || {}; | |
this.php_js.ini = this.php_js.ini || {}; | |
// END REDUNDANT | |
switch ((this.php_js.ini['unicode.semantics'] && this.php_js.ini['unicode.semantics'].local_value.toLowerCase())) { | |
case 'on': | |
// Full-blown Unicode including non-Basic-Multilingual-Plane characters | |
// strlen() | |
for (i = 0; i < str.length; i++) { | |
if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) { | |
allBMP = false; | |
break; | |
} | |
} | |
if (!allBMP) { | |
if (start < 0) { | |
for (i = end - 1, es = (start += end); i >= es; i--) { | |
if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) { | |
start--; | |
es--; | |
} | |
} | |
} else { | |
var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; | |
while ((surrogatePairs.exec(str)) !== null) { | |
var li = surrogatePairs.lastIndex; | |
if (li - 2 < start) { | |
start++; | |
} else { | |
break; | |
} | |
} | |
} | |
if (start >= end || start < 0) { | |
return false; | |
} | |
if (len < 0) { | |
for (i = end - 1, el = (end += len); i >= el; i--) { | |
if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) { | |
end--; | |
el--; | |
} | |
} | |
if (start > end) { | |
return false; | |
} | |
return str.slice(start, end); | |
} else { | |
se = start + len; | |
for (i = start; i < se; i++) { | |
ret += str.charAt(i); | |
if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) { | |
// Go one further, since one of the "characters" is part of a surrogate pair | |
se++; | |
} | |
} | |
return ret; | |
} | |
break; | |
} | |
// Fall-through | |
break; | |
case 'off': | |
// assumes there are no non-BMP characters; | |
// if there may be such characters, then it is best to turn it on (critical in true XHTML/XML) | |
break; | |
default: | |
if (start < 0) { | |
start += end; | |
} | |
end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start); | |
// PHP returns false if start does not fall within the string. | |
// PHP returns false if the calculated end comes before the calculated start. | |
// PHP returns an empty string if start and end are the same. | |
// Otherwise, PHP returns the portion of the string from start to end. | |
return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end); | |
} | |
// Please Netbeans | |
return undefined; | |
} | |
function num_to_letras($numero, $moneda, $subfijo){ | |
$xarray ={"0":"CERO","1":"UN","2":"DOS","3":"TRES","4":"CUATRO","5":"CINCO","6":"SEIS", | |
"7":"SIETE","8":"OCHO","9":"NUEVE","10":"DIEZ","11":"ONCE","12":"DOCE","13":"TRECE", | |
"14":"CATORCE","15":"QUINCE","16":"DIECISEIS","17":"DIECISIETE","18":"DIECIOCHO", | |
"19":"DIECINUEVE","20":"VEINTI","30":"TREINTA","40":"CUARENTA","50":"CINCUENTA", | |
"60":"SESENTA","70":"SETENTA","80":"OCHENTA","90":"NOVENTA","100":"CIENTO", | |
"200":"DOSCIENTOS","300":"TRESCIENTOS","400":"CUATROCIENTOS","500":"QUINIENTOS", | |
"600":"SEISCIENTOS","700":"SETECIENTOS","800":"OCHOCIENTOS","900":"NOVECIENTOS"}; | |
$numero = $numero.trim(); | |
$xpos_punto = $numero.indexOf("."); | |
$xaux_int = $numero; | |
$xdecimales = '00'; | |
if ($xpos_punto >= 0) | |
{ | |
$xpos_punto = $numero.indexOf('.'); | |
$xaux_numero = $numero.split('.'); | |
if($xpos_punto === 0 ) | |
{ | |
$xaux_int = '0'; | |
$xdecimales = $xaux_numero[1]; | |
} | |
else | |
{ | |
$xaux_int = $xaux_numero[0];// obtengo el entero de la cifra a covertir | |
$xdecimales = $xaux_numero[1];// obtengo los valores decimales | |
} | |
} | |
$XAUX = my_str_pad($xaux_int); | |
$xcadena = ''; | |
$xseek = ''; | |
for($xz = 0; $xz < 3; $xz++) | |
{ | |
$xaux = substr($XAUX, $xz*6, 6); | |
$xi = 0; | |
$xlimite = 6;// inicializo el contador de centenas xi y establezco el límite a 6 dígitos en la parte entera | |
$xexit = true;// bandera para controlar el ciclo del While | |
while ($xexit) | |
{ | |
if ($xi == $xlimite) { // si ya llegó al límite máximo de enteros | |
break; // termina el ciclo | |
} | |
$x3digitos = ($xlimite - $xi) * -1; | |
$xaux = substr($xaux, $x3digitos, my_abs($x3digitos)); | |
for($xy = 1; $xy < 4; $xy++){ | |
switch($xy) | |
{ | |
case 1: | |
$key = parseInt( substr($xaux, 0, 3), 10); | |
$key = ( isNaN($key) ) ? 0 : $key; | |
if (100 > $key) { // si el grupo de tres dígitos es menor a una centena ( < 99) no hace nada y pasa a revisar las decenas | |
/* do nothing */ | |
} | |
else | |
{ | |
if($key in $xarray){ | |
$xseek = $xarray[$key]; | |
$xsub = subfijo($xaux); | |
$xcadena = (100 == $key) ? ' ' + $xcadena + ' CIEN ' + $xsub : ' ' + $xcadena + ' ' + $xseek + ' ' + $xsub ; | |
$xy = 3; | |
} | |
else | |
{ | |
$key = (parseInt(substr($xaux, 0, 1), 10)) * 100; | |
$xseek = $xarray[$key]; | |
$xcadena = ' ' + $xcadena + ' ' + $xseek; | |
} | |
} | |
break; | |
case 2: | |
$key = parseInt( substr($xaux, 1, 2), 10); | |
$key = ( isNaN($key) ) ? 0 : $key; | |
if (10 > $key) { | |
/* do nothing */ | |
} | |
else | |
{ | |
if($key in $xarray) | |
{ | |
$xseek = $xarray[$key]; | |
$xsub = subfijo($xaux); | |
$xcadena = (20 == $key) ? ' ' + $xcadena + ' VEINTE ' + $xsub : ' ' + $xcadena + ' ' + $xseek + ' ' + $xsub ; | |
$xy = 3; | |
} | |
else | |
{ | |
$key = parseInt( substr($xaux, 1, 1) , 10) * 10; | |
$xseek = $xarray[$key]; | |
$xcadena = (20 == $key) ? ' ' + $xcadena + ' ' + $xseek : ' ' + $xcadena + ' ' + $xseek + ' Y '; | |
} | |
} | |
break; | |
case 3: | |
$key = parseInt(substr($xaux, 2, 1), 10); | |
$key = ( isNaN($key) ) ? 0 : $key; | |
if (1 > $key) | |
{ // si la unidad es cero, ya no hace nada | |
/* do nothing */ | |
} | |
else | |
{ | |
$xseek = $xarray[$key]; | |
$xsub = subfijo($xaux); | |
$xcadena = ' ' + $xcadena + ' ' + $xseek + ' ' + $xsub; | |
} | |
break; | |
} | |
} | |
$xi += 3; | |
} | |
if( ('ILLON' == substr($xcadena.trim(), -5, 5) ) || ('ILLONES' == substr($xcadena.trim(), -7, 7)) ) | |
$xcadena += ' DE'; | |
if('' !== $xaux.trim()) { | |
switch($xz) | |
{ | |
case 0: | |
$xcadena += ( '1' == substr($XAUX, $xz * 6, 6).trim() ) ? 'UN BILLON ' : ' BILLONES '; | |
break; | |
case 1: | |
$xcadena += ( '1' == substr($XAUX, $xz * 6, 6).trim() ) ? 'UN MILLON ' : ' MILLONES '; | |
break; | |
case 2: | |
if(parseInt($numero, 10) === 0) | |
$xcadena = 'CERO ' + $moneda + 'S ' + $xdecimales + '/100 ' + $subfijo; | |
else if(parseInt($numero, 10) === 1) | |
$xcadena = 'UN ' + $moneda + ' ' + $xdecimales + '/100 ' + $subfijo; | |
else | |
$xcadena += ' ' + $moneda + 'S ' + $xdecimales + '/100 ' + $subfijo; | |
break; | |
} | |
} | |
$xcadena = $xcadena.replace(/VEINTI /g, 'VEINTI'); | |
$xcadena = $xcadena.replace(/UN UN/g, 'UN' ); | |
$xcadena = $xcadena.replace(/ /g, ' '); | |
$xcadena = $xcadena.replace(/BILLON DE MILLONES/g, 'BILLON DE'); | |
$xcadena = $xcadena.replace(/BILLONES DE MILLONES/g, 'BILLONES DE'); | |
$xcadena = $xcadena.replace(/DE UN/g,'UN' ); | |
} | |
return $xcadena.trim(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment