Nueva pregunta

Pregunta:

Fecha: 23-08-2019 06:40:26 (En Español)

Convertir Numeros a Letras[No resuelta]

Hola con todos, por el presente hacerles llegar un saludo cordial a todo los integrantes de PHPCentral, y a su vez solicitarles sus aportes y/o comentarios, lo que deseo convertir un numero en letras, (los que van en las facturas, el monto total, en letras, mi entorno de trabajo es de php,

quedo a la espera de sus comentarios.
Etiquetas: HTML - Javascript - MySQL - PHP - Pregunta Votos: 0 - Respuestas: 3 - Vistas: 20 Compartir en: Google Facebook Twitter LinkedIn Link
 

Respuestas:

  • Fecha: 23-08-2019 14:23:52 Hola, puedes usar esta función que a mí me va de maravillas.

    
    			function SubValLetra($numero){ //$numero es el valor que quieres convertir a letras
    			    $Ptr="";
    			    $n=0;
    			    $i=0;
    			    $x ="";
    			    $Rtn ="";
    			    $Tem ="";
    
    			    $x = trim("$numero");
    			    $n = strlen($x);
    
    			    $Tem = $this->Void;
    			    $i = $n;
    
    			    while( $i > 0){
    			       $Tem = $this->Parte(intval(substr($x, $n - $i, 1).
    			                           str_repeat($this->Zero, $i - 1 )));
    			       If( $Tem != "Cero" )
    			          $Rtn .= $Tem . $this->SP;
    					  $i = $i - 1;
    			    }
    
    
    			    //--------------------- GoSub FiltroMil ------------------------------
    			    $Rtn=str_replace(" Mil Mil", " Un Mil", $Rtn );
    			    while(1){
    			       	$Ptr = strpos($Rtn, "Mil ");
    				   	If(!($Ptr===false)){
    			       		If(! (strpos($Rtn, "Mil ",$Ptr + 1) === false ))
    			            	$this->ReplaceStringFrom($Rtn, "Mil ", "", $Ptr);
    						Else
    							break;
    			       	}
    			       	else break;
    			    }
    
    			    //--------------------- GoSub FiltroCiento ------------------------------
    			    $Ptr = -1;
    			    do{
    			       $Ptr = strpos($Rtn, "Cien ", $Ptr+1);
    			       if(!($Ptr===false)){
    			          	$Tem = substr($Rtn, $Ptr + 5 ,1);
    					  	if( $Tem == "M" || $Tem == $this->Void);
    					  	else
    			            $this->ReplaceStringFrom($Rtn, "Cien", "Ciento", $Ptr);
    			       }
    			    }
    			    while(!($Ptr === false));
    
    			    //--------------------- FiltroEspeciales ------------------------------
    			    $Rtn=str_replace("Diez Un", "Once", $Rtn );
    			    $Rtn=str_replace("Diez Dos", "Doce", $Rtn );
    			    $Rtn=str_replace("Diez Tres", "Trece", $Rtn );
    			    $Rtn=str_replace("Diez Cuatro", "Catorce", $Rtn );
    			    $Rtn=str_replace("Diez Cinco", "Quince", $Rtn );
    			    $Rtn=str_replace("Diez Seis", "Dieciseis", $Rtn );
    			    $Rtn=str_replace("Diez Siete", "Diecisiete", $Rtn );
    			    $Rtn=str_replace("Diez Ocho", "Dieciocho", $Rtn );
    			    $Rtn=str_replace("Diez Nueve", "Diecinueve", $Rtn );
    			    $Rtn=str_replace("Veinte Un", "Veintiun", $Rtn );
    			    $Rtn=str_replace("Veinte Dos", "Veintidos", $Rtn );
    			    $Rtn=str_replace("Veinte Tres", "Veintitres", $Rtn );
    			    $Rtn=str_replace("Veinte Cuatro", "Veinticuatro", $Rtn );
    			    $Rtn=str_replace("Veinte Cinco", "Veinticinco", $Rtn );
    			    $Rtn=str_replace("Veinte Seis", "Veintiseís", $Rtn );
    			    $Rtn=str_replace("Veinte Siete", "Veintisiete", $Rtn );
    			    $Rtn=str_replace("Veinte Ocho", "Veintiocho", $Rtn );
    			    $Rtn=str_replace("Veinte Nueve", "Veintinueve", $Rtn );
    
    			    //--------------------- FiltroUn ------------------------------
    			    If(substr($Rtn,0,1) == "M") $Rtn = "Un " . $Rtn;
    			    //--------------------- Adicionar Y ------------------------------
    			    for($i=65; $i<=88; $i++){
    			      	If($i != 77)
    			         	$Rtn=str_replace("a " . Chr($i), "* y " . Chr($i), $Rtn);
    			    }
    			    $Rtn=str_replace("*", "a" , $Rtn);
    			    return($Rtn);
    			}
    
    
    			function ReplaceStringFrom(&$x, $OldWrd, $NewWrd, $Ptr){
    				$x = substr($x, 0, $Ptr)  . $NewWrd . substr($x, strlen($OldWrd) + $Ptr);
    			}
    
    
    			function Parte($x){
    			    $Rtn='';
    			    $t='';
    			    $i='';
    			    Do{
    					switch($x){
    				         Case 0:  $t = "Cero";break;
    				         Case 1:  $t = "Uno";break;
    				         Case 2:  $t = "Dos";break;
    				         Case 3:  $t = "Tres";break;
    				         Case 4:  $t = "Cuatro";break;
    				         Case 5:  $t = "Cinco";break;
    				         Case 6:  $t = "Seis";break;
    				         Case 7:  $t = "Siete";break;
    				         Case 8:  $t = "Ocho";break;
    				         Case 9:  $t = "Nueve";break;
    				         Case 10: $t = "Diez";break;
    				         Case 20: $t = "Veinte";break;
    				         Case 30: $t = "Treinta";break;
    				         Case 40: $t = "Cuarenta";break;
    				         Case 50: $t = "Cincuenta";break;
    				         Case 60: $t = "Sesenta";break;
    				         Case 70: $t = "Setenta";break;
    				         Case 80: $t = "Ochenta";break;
    				         Case 90: $t = "Noventa";break;
    				         Case 100: $t = "Cien";break;
    				         Case 200: $t = "Doscientos";break;
    				         Case 300: $t = "Trescientos";break;
    				         Case 400: $t = "Cuatrocientos";break;
    				         Case 500: $t = "Quinientos";break;
    				         Case 600: $t = "Seiscientos";break;
    				         Case 700: $t = "Setecientos";break;
    				         Case 800: $t = "Ochocientos";break;
    				         Case 900: $t = "Novecientos";break;
    				         Case 1000: $t = "Mil";break;
    				         Case 1000000: $t = "Millón";break;
    			      	}
    
    				  	If($t == $this->Void){
    			        	$i = $i + 1;
    						$x = $x / 1000;
    						If($x== 0) $i = 0;
    			      	}
    				  	else
    			        	break;
    
    			    }
    			    while($i != 0);
    
    			    $Rtn = $t;
    			    Switch($i){
    			       Case 0: $t = $this->Void;break;
    			       Case 1: $t = " Mil";break;
    			       Case 2: $t = " Millones";break;
    			       Case 3: $t = " Billones";break;
    			    }
    			    return($Rtn . $t);
    			}
    
    		}
    
    		//-------------- Programa principal ----------------------
    
    		$V=new EnLetras();
    
    		 utf8_decode($con_letra=strtolower($V->ValorEnLetras($total_c,"")) ); // aquí ya te da el resultado en letras
    


    Espero te funcione.

    Saludos

    Joaquín Vides
      Votos: 0 - Link respuesta
     
  • Fecha: 28-08-2019 09:28:18 Hola,
    También pudieras utilizar algo más estándar como la clase NumberFormatter de PHP que es parte de la extensión intl.

    Con eso pudieras hacer algo tan sencillo como:

    $f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
    echo $f->format(1432);
    


    Recuerda que es parte de la extensión intl y la debes habilitar para poder utilizar la función.

    Saludos
      Votos: 1 - Link respuesta
     
  • Fecha: 05-08-2023 15:33:10 Buenas noches

    Utilizando PHP y FPDF me encuentro con el siguiente inconveniente:

    // CÓDIGO NÚMEROS A LETRAS
    
    $formatterES = new NumberFormatter("es-ES", NumberFormatter::SPELLOUT);
    $izquierda = intval(floor($total));
    $derecha = intval(($total - floor($total)) * 100);
    
    $fpdf->Ln(1);
    $fpdf->SetFont('Arial','B',8);
    $fpdf->SetTextColor('0','0','0');
    $fpdf->SetFillColor(244, 246, 246);
    $fpdf->Cell(5);
    $fpdf->Cell(16, 5, utf8_decode('Son Pesos: '), 0, 0, 'L', true);
    
    $fpdf->SetFont('Arial','I',8);
    $fpdf->SetTextColor('0','0','0');
    $fpdf->SetFillColor(244, 246, 246);
    //$fpdf->Cell(165, 5, utf8_decode('En construccion'), 0, 1, 'L', true);
    $fpdf->Cell(165, 5, utf8_decode($formatterES->format($izquierda).' con '.$formatterES->format($derecha).' centavos '), 0, 1, 'L', true);
    


    Si utilizo el navegador local (Wampeserver), funciona de maravillar, si lo subo a la WEB, NO FUNCIONA
    Si la linea 18, la la comento, funciona en la web, ero no me agrega el numero a letra.
    Alguien que tenga una explicación al respecto, le estaré agradeciado
      Votos: 0 - Link respuesta
     
Para participar activamente de la comunidad primero debes autenticarte, ingresa al sistema.Iniciar Sesión
 
frjcbbae garagebible.com