Comunidad de diseño web y desarrollo en internet online

Convertir importe(numero) a importe en letras

Citar            
MensajeEscrito el 30 May 2009 03:43 pm
Hola que tal señores como estan, aqui con una duda... no se si exista una funcion para convertir el importe(numero) de una factura a importe en letras por ejemplo $233 (doscientos treinta y tres ), si saben de alguna funcion que me pudiera ayudar se los agradeceria mucho, de antemos gracias, saludos cordiales.

Por pepe84

26 de clabLevel



 

firefox
Citar            
MensajeEscrito el 30 May 2009 11:30 pm
Que yo sepa no existe, es mas si existiera tendrian que crearla para cada idioma de mundo y parte del extrangero, o solo estaria en ingles. Pero puedes crearte una tu mismo. Que te devuelva el string de los numeros escritos. Osea, separas todos lols numeros en un array coges el primero y con un if-else-if vas imprimiendo "ciento" si es 1 "doscientos" si es dos etc etc etc. Es digno de un metodo que dependa de algun objeto super mega cO0l :cool: .

Por Atomsk

350 de clabLevel

3 tutoriales

 

firefox
Citar            
MensajeEscrito el 01 Jun 2009 06:49 pm
Ok gracias, si tengo mas o menos la idea, solamente preguntaba por si ya existia alguna para ahorrarme el trabajo jeje.

Por pepe84

26 de clabLevel



 

firefox
Citar            
MensajeEscrito el 21 Dic 2009 10:33 pm
Gente, les dejo la funcion para PHP


Código PHP :


// Función $modulo, regresa el residuo de una división 
function mod($dividendo , $divisor) 
{ 
  $resDiv = $dividendo / $divisor ;  
  
  $parteEnt = floor($resDiv);            // Obtiene la parte Entera de $resDiv 
  
  $parteFrac = $resDiv - $parteEnt ;      // Obtiene la parte Fraccionaria de la división
  
  $modulo = round($parteFrac * $divisor);  // Regresa la parte fraccionaria * la división ($modulo) 
  return $modulo; 
} // Fin de función mod





// Función ObtenerParteEntDiv, regresa la parte entera de una división
function ObtenerParteEntDiv($dividendo , $divisor) 
{ 
  $resDiv = $dividendo / $divisor ;  
  $parteEntDiv = floor($resDiv); 
  return $parteEntDiv; 
} // Fin de función ObtenerParteEntDiv





// function fraction_part, regresa la parte Fraccionaria de una cantidad
function fraction_part($dividendo , $divisor) 
{ 
  $resDiv = $dividendo / $divisor ;  
  $f_part = floor($resDiv); 
  return $f_part; 
} // Fin de función fraction_part


// function string_literal conversion is the core of this program 
// converts $numbers to spanish strings, handling the general special 
// cases in spanish language. 
function string_literal_conversion($number) 
{   
   // first, divide your $number in hundreds, tens and units, cascadig 
   // trough subsequent divisions, using the modulus of each division 
   // for the next. 

   $centenas = ObtenerParteEntDiv($number, 100); 
   
   $number = $number % 100; 

   $decenas = ObtenerParteEntDiv($number, 10); 
   $number = $number%10; 

   $unidades = ObtenerParteEntDiv($number, 1); 
   $number = $number % 1;  
   $string_hundreds="";
   $string_tens="";
   $string_units="";

   // cascade trough hundreds. This will convert the hundreds part to 
   // their corresponding string in spanish.
   if($centenas == 1) $string_hundreds = "ciento ";
   if($centenas == 2) $string_hundreds = "doscientos ";
   if($centenas == 3) $string_hundreds = "trescientos ";
   if($centenas == 4) $string_hundreds = "cuatrocientos ";
   if($centenas == 5) $string_hundreds = "quinientos ";
   if($centenas == 6) $string_hundreds = "seiscientos ";
   if($centenas == 7) $string_hundreds = "setecientos ";
   if($centenas == 8) $string_hundreds = "ochocientos ";
   if($centenas == 9) $string_hundreds = "novecientos ";

   
 // end switch hundreds 

   // casgade trough tens. This will convert the tens part to corresponding 
   // strings in spanish. Note, however that the strings between 11 and 19 
   // are all special cases. Also 21-29 is a special case in spanish. 
   if($decenas == 1){
      //Special case, depends on units for each conversion
      if($unidades == 1){
         $string_tens = "once";
      }
      
      if($unidades == 2){
         $string_tens = "doce";
      }
      
      if($unidades == 3){
         $string_tens = "trece";
      }
      
      if($unidades == 4){
         $string_tens = "catorce";
      }
      
      if($unidades == 5){
         $string_tens = "quince";
      }
      
      if($unidades == 6){
         $string_tens = "dieciseis";
      }
      
      if($unidades == 7){
         $string_tens = "diecisiete";
      }
      
      if($unidades == 8){
         $string_tens = "dieciocho";
      }
      
      if($unidades == 9){
         $string_tens = "diecinueve";
      }
   } 
   //alert("$string_tens ="+$string_tens);
   
   if($decenas == 2){
      $string_tens = "veinti";
   }
   if($decenas == 3){
      $string_tens = "treinta";
   }
   if($decenas == 4){
      $string_tens = "cuarenta";
   }
   if($decenas == 5){
      $string_tens = "cincuenta";
   }
   if($decenas == 6){
      $string_tens = "sesenta";
   }
   if($decenas == 7){
      $string_tens = "setenta";
   }
   if($decenas == 8){
      $string_tens = "ochenta";
   }
   if($decenas == 9){
      $string_tens = "noventa";
   }
   
    // Fin de swicth $decenas


   // cascades trough units, This will convert the units part to corresponding 
   // strings in spanish. Note however that a check is being made to see wether 
   // the special cases 11-19 were used. In that case, the whole conversion of 
   // individual units is ignored since it was already made in the tens cascade. 

   if ($decenas == 1) 
   { 
      $string_units="";  // empties the units check, since it has alredy been handled on the tens switch 
   } 
   else 
   { 
      if($unidades == 1){
         $string_units = "un";
      }
      if($unidades == 2){
         $string_units = "dos";
      }
      if($unidades == 3){
         $string_units = "tres";
      }
      if($unidades == 4){
         $string_units = "cuatro";
      }
      if($unidades == 5){
         $string_units = "cinco";
      }
      if($unidades == 6){
         $string_units = "seis";
      }
      if($unidades == 7){
         $string_units = "siete";
      }
      if($unidades == 8){
         $string_units = "ocho";
      }
      if($unidades == 9){
         $string_units = "nueve";
      }
       // end switch units 
   } // end if-then-else 
   

//final special cases. This conditions will handle the special cases which 
//are not as general as the ones in the cascades. Basically four: 

// when you've got 100, you dont' say 'ciento' you say 'cien' 
// 'ciento' is used only for [101 >= $number > 199] 
if ($centenas == 1 && $decenas == 0 && $unidades == 0) 
{ 
   $string_hundreds = "cien " ; 
}  

// when you've got 10, you don't say any of the 11-19 special 
// cases.. just say 'diez' 
if ($decenas == 1 && $unidades ==0) 
{ 
   $string_tens = "diez " ; 
} 

// when you've got 20, you don't say 'veinti', which is used 
// only for [21 >= $number > 29] 
if ($decenas == 2 && $unidades ==0) 
{ 
  $string_tens = "veinte " ; 
} 

// for $numbers >= 30, you don't use a single word such as veintiuno 
// (twenty one), you must add 'y' (and), and use two words. v.gr 31 
// 'treinta y uno' (thirty and one) 
if ($decenas >=3 && $unidades >=1) 
{ 
   $string_tens = $string_tens." y "; 
} 

// this line gathers all the hundreds, tens and units into the final string 
// and returns it as the function value.
$final_string = $string_hundreds.$string_tens.$string_units;


return $final_string ; 

} //end of function string_literal_conversion()================================ 

// handle some external special cases. Specially the $millions, $thousands 
// and hundreds $descriptors. Since the same rules apply to all $number triads 
// descriptions are handled outside the string conversion function, so it can 
// be re used for each triad. 


function convertir_numeros_a_letras($number)
{
   
  //$number = $number_format ($number, 2);
   $number1=$number;
   //settype ($number, "integer");
   $cent = explode ('.',$number1);   
   $centavos = $cent[1];
   $numerparchado=$cent[0];
   if ($centavos == 0 || $centavos == undefined){ $centavos = "00";}

   if ($number == 0 || $number == "") 
   { // if amount = 0, then forget all about conversions, 
      $centenas_final_string=" cero "; // amount is zero (cero). handle it externally, to 
      // function breakdown 
  } 
   else 
   { 
   
     $millions  = ObtenerParteEntDiv($number, 1000000); // first, send the $millions to the string 
      $number = mod($numerparchado, 1000000);           // conversion function 
      
     if ($millions != 0)
      {                      
      // This condition handles the plural case 
         if ($millions == 1) 
         {              // if only 1, use 'millon' (million). if 
            $descriptor= " millon ";  // > than 1, use 'millones' ($millions) as 
            } 
         else 
         {                           // a $descriptor for this triad. 
              $descriptor = " millones "; 
            } 
      } 
      else 
      {    
         $descriptor = " ";                 // if 0 million then use no $descriptor. 
      } 
      
     $millions_final_string = string_literal_conversion($millions).$descriptor; 
          
      
      $thousands = ObtenerParteEntDiv($number, 1000);  // now, send the $thousands to the string 
        $number = $number % 1000 ;            // conversion function. 
      //print "Th:".$thousands;
      
      
     if ($thousands != 1) 
      {                   // This condition eliminates the $descriptor 
         $thousands_final_string =string_literal_conversion($thousands) . " mil "; 
       //  $descriptor = " mil ";          // if there are no $thousands on the amount 
      } 
      if ($thousands == 1)
      {
         $thousands_final_string = " mil "; 
     }
      if ($thousands < 1) 
      { 
         $thousands_final_string = " "; 
      } 
  
      // this will handle $numbers between 1 and 999 which 
      // need no $descriptor whatsoever. 

      $centenas  = $number;                     
      $centenas_final_string = string_literal_conversion($centenas) ; 
      
   } //end if ($number ==0) 


   //finally, print the output. 

   /* Concatena los millones, miles y cientos*/
   $cad = $millions_final_string.$thousands_final_string.$centenas_final_string; 
   
   /* Convierte la cadena a Mayúsculas*/
   $cad = strtoupper($cad);       

   if (strlen($centavos)>2)
   {   
      if(substr($centavos,2,3)>= 5){
         $centavos = substr($centavos,0,1).(intval(substr($centavos,1,2))+1);
      }   else{
        $centavos = substr($centavos,0,2);
       }
   }

   /* Concatena a los $centavos la cadena "/100" */
   if (strlen($centavos)==1)
   {
      $centavos = $centavos."0";
   }
   $centavos = $centavos. "/100"; 


   /* Asigna el tipo de $moneda, para 1 = PESO, para distinto de 1 = PESOS*/
   if ($number == 1)
   {
      $moneda = " PESO ";  
   }
   else
   {
      $moneda = " PESOS ";  
   }
   /* Regresa el número en cadena entre paréntesis y con tipo de $moneda y la fase M.N.*/
   return( $cad.$moneda.$centavos." Pesos MX ");
}


Por vousys

1 de clabLevel



 

opera
Citar            
MensajeEscrito el 23 Sep 2010 03:00 pm
me puedes ayudar no se como intergrarlo o llamarlo desde mi flash

Por adanniel

6 de clabLevel



Genero:Masculino  

Mexico

msie8
Citar            
MensajeEscrito el 23 Sep 2010 03:05 pm
Encontre este Codigo tambien esta bueno pero el problema esta que como no soy bueno en Flash no se como enviarle las variables no tampoco se como flash puede tomar ya el monto en letra.

Si me pudieran ayudar se lo agradeceria

Código PHP :

<?PHP
/**
 * OEOG Class para convertir numeros en palabras 
 * 
 * 
 * @version   $Id: CNumeroaLetra.php,v 1.0.1 2004-10-29 13:20 ortizom Exp $
 * @author    Omar Eduardo Ortiz Garza <[email protected]>
 *                    gracias a Alberto Gonzalez por su contribucion para corregir
 *                    dos errores
 * @copyright (c) 2004-2005 Omar Eduardo Ortiz Garza
 * @since     Friday, October 29, 2004
 **/
/***************************************************************************
 *
 *  Este programa es software libre; puedes redistribuir y/o modificar
 *  bajo los terminos de la GNU General Public License como se publico por
 *  la Free Software Foundation; version 2 de la Licencia, o cualquier
 *  (a tu eleccion) version posterior.
 *
 ***************************************************************************/

/***************************************************************************
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 ***************************************************************************/

class CNumeroaLetra{
/***************************************************************************
 *
 *   Propiedades:
 *   $numero:   Es la cantidad a ser convertida a letras maximo 999,999,999,999.99
 *   $genero:   0 para femenino y 1 para masculino, es util dependiendo de la
 *            moneda ej: cuatrocientos pesos / cuatrocientas pesetas
 *   $moneda:   nombre de la moneda
 *   $prefijo:   texto a imprimir antes de la cantidad 
 *   $sufijo:   texto a imprimir despues de la cantidad
 *            tanto el $sufijo como el $prefijo en la impresion de cheques o
 *            facturas, para impedir que se altere la cantidad
 *   $mayusculas: 0 para minusculas, 1 para mayusculas indica como debe 
 *            mostrarse el texto
 *   $textos_posibles: contiene todas las posibles palabras a usar
 *   $aTexto:   es el arreglo de los textos que se usan de acuerdo al genero 
 *            seleccionado
 *
 ***************************************************************************/

   private $numero=0;
   private $genero=1;
   private $moneda="PESOS";
   private $prefijo="(***";
   private $sufijo="***)";
   private $mayusculas=1;
   //textos
   private $textos_posibles= array(
   0 => array ('UNA ','DOS ','TRES ','CUATRO ','CINCO ','SEIS ','SIETE ','OCHO ','NUEVE ','UN '),
   1 => array ('ONCE ','DOCE ','TRECE ','CATORCE ','QUINCE ','DIECISEIS ','DIECISIETE ','DIECIOCHO ','DIECINUEVE ',''),
   2 => array ('DIEZ ','VEINTE ','TREINTA ','CUARENTA ','CINCUENTA ','SESENTA ','SETENTA ','OCHENTA ','NOVENTA ','VEINTI'),
   3 => array ('CIEN ','DOSCIENTAS ','TRESCIENTAS ','CUATROCIENTAS ','QUINIENTAS ','SEISCIENTAS ','SETECIENTAS ','OCHOCIENTAS ','NOVECIENTAS ','CIENTO '),
        4 => array ('CIEN ','DOSCIENTOS ','TRESCIENTOS ','CUATROCIENTOS ','QUINIENTOS ','SEISCIENTOS ','SETECIENTOS ','OCHOCIENTOS ','NOVECIENTOS ','CIENTO '),
   5 => array ('MIL ','MILLON ','MILLONES ','CERO ','Y ','UNO ','DOS ','CON ','','')
   );
   private $aTexto;

/***************************************************************************
 *
 *   Metodos:
 *   _construct:   Inicializa textos
 *   setNumero:   Asigna el numero a convertir a letra
 *  setPrefijo:   Asigna el prefijo
 *   setSufijo:   Asiga el sufijo
 *   setMoneda:   Asigna la moneda
 *   setGenero:   Asigan genero 
 *   setMayusculas:   Asigna uso de mayusculas o minusculas
 *   letra:      Convierte numero en letra
 *   letraUnidad: Convierte unidad en letra, asigna miles y millones
 *   letraDecena: Contiene decena en letra
 *   letraCentena: Convierte centena en letra
 *
 ***************************************************************************/   
   function __construct(){
      for($i=0; $i<6;$i++)
            for($j=0;$j<10;$j++)
            $this->aTexto[$i][$j]=$this->textos_posibles[$i][$j];
   }

   function setNumero($num){
      $this->numero=(double)$num;
   }

   function setPrefijo($pre){
      $this->prefijo=$pre;
   }

   function setSufijo($sub){
      $this->sufijo=$sub;
   }

   function setMoneda($mon){
      $this->moneda=$mon;
   }

   function setGenero($gen){
      $this->genero=(int)$gen;
   }

   function setMayusculas($may){
      $this->mayusculas=(int)$may;
   }

   function letra(){
      if($this->genero==1){ //masculino
         $this->aTexto[0][0]=$this->textos_posibles[5][5];
         for($j=0;$j<9;$j++)
               $this->aTexto[3][$j]= $this->aTexto[4][$j];

      }else{//femenino
         $this->aTexto[0][0]=$this->textos_posibles[0][0];
         for($j=0;$j<9;$j++)
               $this->aTexto[3][$j]= $this->aTexto[3][$j];
      }

      $cnumero=sprintf("%015.2f",$this->numero);
      $texto="";
      if(strlen($cnumero)>15){
      $texto="Excede tamaño permitido";
      }else{
         $hay_significativo=false;
         for ($pos=0; $pos<12; $pos++){
            // Control existencia Dígito significativo 
               if (!($hay_significativo)&&(substr($cnumero,$pos,1) == '0')) ;
               else $hay_dignificativo = true;

               // Detectar Tipo de Dígito 
               switch($pos % 3) {
                  case 0: $texto.=$this->letraCentena($pos,$cnumero); break;
                  case 1: $texto.=$this->letraDecena($pos,$cnumero); break;
                  case 2: $texto.=$this->letraUnidad($pos,$cnumero); break;
            }
         }
            // Detectar caso 0 
            if ($texto == '') $texto = $this->aTexto[5][3];
         if($this->mayusculas){//mayusculas
            $texto=strtoupper($this->prefijo.$texto." ".$this->moneda." ".substr($cnumero,-2)."/100 ".$this->sufijo);   
         }else{//minusculas
            $texto=strtolower($this->prefijo.$texto." ".$this->moneda." ".substr($cnumero,-2)."/100 ".$this->sufijo);   
         }
      }
      return $texto;

   }

   public function __toString() {
      return $this->letra();
   }

   //traducir letra a unidad
   private function letraUnidad($pos,$cnumero){
      $unidad_texto="";
         if( !((substr($cnumero,$pos,1) == '0') || 
               (substr($cnumero,$pos - 1,1) == '1') ||
               ((substr($cnumero, $pos - 2, 3) == '001') &&  (($pos == 2) || ($pos == 8)) ) 
             )
        ){ 
         if((substr($cnumero,$pos,1) == '1') && ($pos <= 6)){
               $unidad_texto.=$this->aTexto[0][9]; 
         }else{
            $unidad_texto.=$this->aTexto[0][substr($cnumero,$pos,1) - 1];
         }
      }
         if((($pos == 2) || ($pos == 8)) && 
         (substr($cnumero, $pos - 2, 3) != '000')){//miles
         if(substr($cnumero,$pos,1)=='1'){
            if($pos <= 6){
               $unidad_texto=substr($unidad_texto,0,-1)." ";
            }else{
               $unidad_texto=substr($unidad_texto,0,-2)." ";
            }
            $unidad_texto.= $this->aTexto[5][0]; 
         }else{
            $unidad_texto.=$this->aTexto[5][0]; 
         }
      }
        if($pos == 5 && substr($cnumero, 0, 6) != '000000'){
         if(substr($cnumero, 0, 6) == '000001'){//millones
           $unidad_texto.=$this->aTexto[5][1];
         }else{
            $unidad_texto.=$this->aTexto[5][2];
         }
      }
      return $unidad_texto;
   }
   //traducir digito a decena
   private function letraDecena($pos,$cnumero){
      $decena_texto="";
         if (substr($cnumero,$pos,1) == '0'){
         return;
      }else if(substr($cnumero,$pos + 1,1) == '0'){ 
            $decena_texto.=$this->aTexto[2][substr($cnumero,$pos,1)-1];
      }else if(substr($cnumero,$pos,1) == '1'){ 
            $decena_texto.=$this->aTexto[1][substr($cnumero,$pos+ 1,1)- 1];
      }else if(substr($cnumero,$pos,1) == '2'){
            $decena_texto.=$this->aTexto[2][9];
      }else{
            $decena_texto.=$this->aTexto[2][substr($cnumero,$pos,1)- 1] . $this->aTexto[5][4];
      }
      return $decena_texto;
      }
   //traducir digito centena
      private function letraCentena($pos,$cnumero){
      $centena_texto="";
         if (substr($cnumero,$pos,1) == '0') return;
         $pos2 = 3;
      if((substr($cnumero,$pos,1) == '1') && (substr($cnumero,$pos+ 1, 2) != '00')){
            $centena_texto.=$this->aTexto[$pos2][9];
         }else{
            $centena_texto.=$this->aTexto[$pos2][substr($cnumero,$pos,1) - 1];
      }
       return $centena_texto;
      
      
   }

}
   echo $texto; 
?>

Por adanniel

6 de clabLevel



Genero:Masculino  

Mexico

msie8
Citar            
MensajeEscrito el 23 Sep 2010 08:13 pm
bueno... si usas AS3 puedes mandar variables por HTTP, esto es un ejemplo bastante sencillo...
var url:String = "http://localhost/prueba";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.nombre = "Pablo";
request.data = variables;
navigateToURL(request);

posteriormente en PHP recoges los datos enviados de la misma forma que lo harías si lo enviaras desde un formulario web
$variable = $_POST['nombre'];

espero que te sirva.

Por pmolina88

74 de clabLevel



Genero:Masculino  

Ingeniero en Sistemas

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.