Convertir números a palabras

Convertir números a palabras

Postby rolando » Tue Oct 06, 2009 4:11 pm

Buenas tardes,

Quisiera saber si existe alguna función que haga una conversión de números a frase de caractéres.

Me explico.

Por ej. enviarle a la función el número "123456", y que me devuelva la frase "ciento veintitrés mil cuatrocientos cincuenta y seis".

Antes de encarar la creación de esta función,consulto si existe o si alguien la ha creado y quisiera compartirla.

Gracias.

Rolando :D
User avatar
rolando
 
Posts: 593
Joined: Sat May 12, 2007 11:47 am
Location: San Nicolás - BA - ARGENTINA

Re: Convertir números a palabras

Postby Lautaro » Tue Oct 06, 2009 4:22 pm

Hola Rolando,

Este codigo lo adapte de no me acuerdo donde, sirve para pesos chilenos y unidades, espero te sirva.
Atte.,

Lautaro Moreira
Osorno
Chile

Code: Select all  Expand view

// Rutinas para Transformar Numeros a Letras
function numtole(lvalor2)
   local temptexv,centav

   temptexv:=""
   centav:=""
   if lvalor2=NIL
     lvalor2:=0
   endif
   if lvalor2<0
      lvalor2:=0-lvalor2
   endif
   temptexv:=alltrim(str(lvalor2))
   if at(".",temptexv)>0
      centav:=substr(temptexv,at(".",temptexv)+1,len(temptexv))
      temptexv:=substr(temptexv,1,at(".",temptexv)-1)
   endif
   temptexv:=strtran(temptexv,"$","")
   temptexv:=strtran(temptexv,".","")
   temptexv:=strtran(temptexv,",","")
   temptexv:=strtran(temptexv," ","")
   temptexv:=alltrim(temptexv)
   if val(temptexv)>0
   nntextva2:=descompnum(temptexv)
   else
   nntextva2:="CERO "
   endif
   if val(centav)>=1
       temptexv:=alltrim(centav)
       temptexv:=descompnum(temptexv)
       nntextva2:=nntextva2+" PESOS CON "+ temptexv+" CENTAVOS"
   else
       nntextva2:=nntextva2+" PESOS"   // + " CON CERO CENTAVOS" // agregar esto si queremos que exprese los 0 cvos.
   endif
   if at("Y UN PESOS CON",nntextva2)>0
   else
       if at("UN PESOS CON",nntextva2)>0
          nntextva2:=strtran(nntextva2,"UN PESOS CON","UN PESO CON")
       endif
   endif
   nntextva2:=strtran(nntextva2,"  "," ")
   nntextva2:=strtran(nntextva2,"MILLONES PESOS","MILLONES DE PESOS")

return(nntextva2)

//----------------------------------------------------------------------//

procedure descompnum(temptexv)
   local nntexva1,nntextva2,lecanum,grunu,temptexv1,tpunact,masmill
   nntextva2:=""
   nntexva1:=""
   temptexv1:=""
   masmill:=.F.
   grunu:=3
   tpunact:="Unidades"
   lecanum:=len(temptexv)
   if lecanum>=10
     masmill:=.T.
   endif
   for lecanum=len(temptexv) to 1 step -1
       temptexv1:=substr(temptexv,lecanum,1)+temptexv1
       if len(temptexv1)=grunu
           nntexva1:=tresdig(temptexv1,tpunact)
           if (tpunact<>"Unidades" .and. temptexv1<>"000") .or. (tpunact<>"Unidades" .and. tpunact<>" MIL " .and. masmill=.T. )
              if tpunact==" MMILLONES "
                  nntextva2:=nntexva1+" MIL "+nntextva2
              else
                  nntextva2:=nntexva1+tpunact+nntextva2
              endif
           else
              nntextva2:=nntexva1+nntextva2
           endif
           tpunact:=caseund(tpunact)
           temptexv1:=""
       endif
   next lecanum

   if len(temptexv1)>0
         nntexva1:=tresdig(temptexv1,tpunact)
         if (tpunact<>"Unidades" .and. temptexv1<>"000") .or. (tpunact<>"Unidades" .and. tpunact<>" MIL " .and. masmill=.T. )
             if tpunact==" MMILLONES "
                nntextva2:=nntexva1+" MIL "+nntextva2
             else
                 nntextva2:=nntexva1+tpunact+nntextva2
             endif
         else
            nntextva2:=nntexva1+nntextva2
         endif
         temptexv1:=""
   endif
   if at("Y UN MILLONES",nntextva2)>0
   else
       if at("UN MILLONES",nntextva2)>0
            nntextva2:=strtran(nntextva2,"UN MILLONES","UN MILLON")
       endif
   endif
   nntextva2:=strtran(nntextva2,"  "," ")
   nntextva2:=strtran(nntextva2,"BILLONES MIL MILLONES","BILLONES")
   nntextva2:=strtran(nntextva2,"BILLONES MIL MILLON","BILLON")
   nntextva2:=strtran(nntextva2,"  "," ")
   //aqui2:=alert(nntexva1)


return(nntextva2)

//----------------------------------------------------------------------//

function tresdig(textva3,tundn)
    local ntextva3,carbu,carbua
    if textva3<>"000"
          textva3:=val(textva3)
          textva3:=alltrim(str(textva3))
          if tundn=NIL
              tundn=""
          endif
          textva3:=alltrim(textva3)
          ntextva3:=""
          if len(textva3)=3
              carbu:=substr(textva3,1,1)
              do case
                  case carbu=="1"
                       if val(substr(textva3,2,3))>0
                           ntextva3:=ntextva3+"CIENTO "
                       else
                           ntextva3:=ntextva3+"CIEN"
                       endif

                  case carbu=="5"
                       ntextva3:=ntextva3+"QUINIENTOS "

                  case carbu=="7"
                       ntextva3:=ntextva3+"SETECIENTOS "

                  case carbu=="9"
                       ntextva3:=ntextva3+"NOVECIENTOS "

                  otherwise
                       ntextva3:=ntextva3+undig(carbu)+"CIENTOS "

              endcase
          endif

          if len(textva3)>=2
              do case
                  case (len(textva3)=3)
                       carbu:=substr(textva3,2,1)
                       carbua:=substr(textva3,3,1)

                  case (len(textva3)=2)
                       carbu:=substr(textva3,1,1)
                       carbua:=substr(textva3,2,1)

              endcase
              do case
                  case carbu=="1"
                        do case
                             case carbua=="0"
                                   ntextva3:=ntextva3+"DIEZ"

                             case carbua=="1"
                                   ntextva3:=ntextva3+"ONCE"

                             case carbua=="2"
                                   ntextva3:=ntextva3+"DOCE"

                             case carbua=="3"
                                   ntextva3:=ntextva3+"TRECE"

                             case carbua=="4"
                                   ntextva3:=ntextva3+"CATORCE"

                             case carbua=="5"
                                   ntextva3:=ntextva3+"QUINCE"

                             case carbua=="6"
                                   ntextva3:=ntextva3+"DIECISEIS"

                             case carbua=="7"
                                   ntextva3:=ntextva3+"DIECISIETE"

                             case carbua=="8"
                                   ntextva3:=ntextva3+"DIECIOCHO"

                             case carbua=="9"
                                   ntextva3:=ntextva3+"DIECINUEVE"

                             otherwise
                                   ntextva3:=ntextva3+"DIEZ"
                        endcase
                  case carbu=="2"
                        ntextva3:=ntextva3+"VEINTE"

                  case carbu=="3"
                        ntextva3:=ntextva3+"TREINTA"

                  case carbu=="4"
                        ntextva3:=ntextva3+"CUARENTA"

                  case carbu=="5"
                        ntextva3:=ntextva3+"CINCUENTA"

                  case carbu=="6"
                        ntextva3:=ntextva3+"SESENTA"

                  case carbu=="7"
                        ntextva3:=ntextva3+"SETENTA"

                  case carbu=="8"
                        ntextva3:=ntextva3+"OCHENTA"

                  case carbu=="9"
                        ntextva3:=ntextva3+"NOVENTA"
              endcase
              if carbu=="1"
              else
                 if carbua="0"
                 else
                    if val(carbu)=2
                        ntextva3:=substr(ntextva3,1,len(ntextva3)-1)+"I"
                    else
                       if carbu<>"0"
                            ntextva3:=ntextva3+" Y "
                       endif
                    endif
                 endif
              endif
          endif

          if carbu=="1"
              if tundn<>"Unidades"
                   ntextva3:=ntextva3
                   *+ tundn
              endif
          else
              if len(textva3)>=1
                   do case
                       case (len(textva3)=3)
                            carbu:=substr(textva3,3,1)

                       case (len(textva3)=2)
                            carbu:=substr(textva3,2,1)

                       case (len(textva3)=1)
                            carbu:=substr(textva3,1,1)

                   endcase
                   if carbu="0"
                       if tundn<>"Unidades"
                           ntextva3:=ntextva3
                           *+ tundn
                       endif
                   else
                       if tundn<>"Unidades"
                           ntextva3:=ntextva3+undig(carbu,tundn)
                           *+ tundn
                       else
                           ntextva3:=ntextva3+undig(carbu,tundn)
                       endif
                   endif
              endif
          endif
          textva3:=ntextva3
    else
        if tundn<>"Unidades"
            textva3:=""
            *+tundn
        else
           textva3:=""
        endif
    endif
    *aqui1:=alert(ntextva3)

return (textva3)

//----------------------------------------------------------------------//

function undig(textva1,tundn)
    if tundn=NIL
         tundn:=""
    endif
    textva1:=alltrim(textva1)
    do case
         case textva1=="1"
               if tundn=="Unidades"
                   *textva1:="UNO"
                   textva1:="UN"
               else
                   textva1:="UN"
               endif

         case textva1=="2"
               textva1:="DOS"

         case textva1=="3"
               textva1:="TRES"

         case textva1=="4"
               textva1:="CUATRO"

         case textva1=="5"
               textva1:="CINCO"

         case textva1=="6"
               textva1:="SEIS"

         case textva1=="7"
               textva1:="SIETE"

         case textva1=="8"
               textva1:="OCHO"

         case textva1=="9"
               textva1:="NUEVE"
    endcase

return(textva1)


//----------------------------------------------------------------------//
function caseund(tpunact)

      do case
          case tpunact=="Unidades"
              tpunact:=" MIL "

          case tpunact==" MIL "
              tpunact:=" MILLONES "

          case tpunact==" MILLONES "
              tpunact:=" MMILLONES "

          case tpunact==" MMILLONES "
              tpunact:=" BILLONES "

      endcase


return (tpunact)


 
User avatar
Lautaro
 
Posts: 322
Joined: Fri Oct 07, 2005 2:44 pm
Location: Osorno, Chile

Re: Convertir números a palabras

Postby rolando » Tue Oct 06, 2009 4:51 pm

Muchas Gracias Lautaro.

Algo similar es lo que busco.

Si no hay algo hecho, adapteré la función que enviaste.

Saludos

Rolando
User avatar
rolando
 
Posts: 593
Joined: Sat May 12, 2007 11:47 am
Location: San Nicolás - BA - ARGENTINA

Re: Convertir números a palabras

Postby ADBLANCO » Tue Oct 06, 2009 4:57 pm

MI FORMA


Code: Select all  Expand view

FUNCTION MONTOESCRITO(MONTO)
LOCAL me_num[45], me_let[45]
LOCAL montonum, montoint, montowrk, montosuf, montoesc, montopos
  me_num[1]=1
  me_let[1]='UN'
  me_num[2]=2
  me_let[2]='DOS'
  me_num[3]=3
  me_let[3]='TRES'
  me_num[4]=4
  me_let[4]='CUATRO'
  me_num[5]=5
  me_let[5]='CINCO'
  me_num[6]=6
  me_let[6]='SEIS'
  me_num[7]=7
  me_let[7]='SIETE'
  me_num[8]=8
  me_let[8]='OCHO'
  me_num[9]=9
  me_let[9]='NUEVE'
  me_num[10]=10
  me_let[10]='DIEZ'
  me_num[11]=11
  me_let[11]='ONCE'
  me_num[12]=12
  me_let[12]='DOCE'
  me_num[13]=13
  me_let[13]='TRECE'
  me_num[14]=14
  me_let[14]='CATORCE'
  me_num[15]=15
  me_let[15]='QUINCE'
  me_num[16]=16
  me_let[16]='DIECISEIS'
  me_num[17]=17
  me_let[17]='DIECISIETE'
  me_num[18]=18
  me_let[18]='DIECIOCHO'
  me_num[19]=19
  me_let[19]='DIECINUEVE'
  me_num[20]=20
  me_let[20]='VEINTE'
  me_num[21]=21
  me_let[21]='VEINTIUN'
  me_num[22]=22
  me_let[22]='VEINTIDOS'
  me_num[23]=23
  me_let[23]='VEINTITRES'
  me_num[24]=24
  me_let[24]='VEINTICUATRO'
  me_num[25]=25
  me_let[25]='VEINTICINCO'
  me_num[26]=26
  me_let[26]='VEINTISEIS'
  me_num[27]=27
  me_let[27]='VEINTISIETE'
  me_num[28]=28
  me_let[28]='VEINTIOCHO'
  me_num[29]=29
  me_let[29]='VEINTINUEVE'
  me_num[30]=30
  me_let[30]='TREINTA'
  me_num[31]=40
  me_let[31]='CUARENTA'
  me_num[32]=50
  me_let[32]='CINCUENTA'
  me_num[33]=60
  me_let[33]='SESENTA'
  me_num[34]=70
  me_let[34]='SETENTA'
  me_num[35]=80
  me_let[35]='OCHENTA'
  me_num[36]=90
  me_let[36]='NOVENTA'
  me_num[37]=100
  me_let[37]='CIENTO'
  me_num[38]=200
  me_let[38]='DOSCIENTOS'
  me_num[39]=300
  me_let[39]='TRESCIENTOS'
  me_num[40]=400
  me_let[40]='CUATROCIENTOS'
  me_num[41]=500
  me_let[41]='QUINIENTOS'
  me_num[42]=600
  me_let[42]='SEISCIENTOS'
  me_num[43]=700
  me_let[43]='SETECIENTOS'
  me_num[44]=800
  me_let[44]='OCHOCIENTOS'
  me_num[45]=900
  me_let[45]='NOVECIENTOS'
  montonum=ltrim(str(monto,11,2))
  montoint=substr(montonum,1,at('.',montonum)-1)
  montowrk=''
  montosuf=''
  montoesc=''
  do while len(montoint)>0
    do case
      case len(montoint)>6
        montowrk=substr(montoint,1,len(montoint)-6)
        montoint=substr(montoint,len(montoint)-5)
        if val(montowrk)>1
          montosuf='MILLONES '
        else
          montosuf='MILLON '
        endif
      case (len(montoint)>3) .and. (len(montoint)<7)
        montowrk=substr(montoint,1,len(montoint)-3)
        montoint=substr(montoint,len(montoint)-2)
        montosuf='MIL '
      case len(montoint)<4
        montowrk=montoint
        montoint=''
        montosuf=''
      otherwise
        montowrk=''
        montosuf=''
    endcase
    if len(montowrk)>0
      do while len(montowrk)>0
        do case
          case len(montowrk)=3
            if val(montowrk)>0
              if val(substr(montowrk,1,1))>0
                montopos=ascan(me_num,val(substr(montowrk,1,1))*100)
                if montowrk='100'
                  montoesc=montoesc+substr(me_let[montopos],1,4)+' '
                else
                  montoesc=montoesc+trim(me_let[montopos])+' '
                endif
              endif
              montowrk=substr(montowrk,2)
            else
              montowrk=''
              montosuf=''
            endif
          case val(montowrk)>29
            montopos=ascan(me_num,val(substr(montowrk,1,1))*10)
            montoesc=montoesc+trim(me_let[montopos])+' '
            montowrk=substr(montowrk,2)
            if val(montowrk)>0
              montoesc=montoesc+'Y '
            endif
          otherwise
            if val(montowrk)>0
              montopos=ascan(me_num,val(montowrk))
              montoesc=montoesc+trim(me_let[montopos])+' '
            endif
            montowrk=''
        endcase
      enddo
      montoesc=montoesc+montosuf
    endif
  enddo
  if val(montonum)>0
    if val(montonum)>=1
      montoesc=montoesc+'BOLIVAR'
      if int(val(montonum))>1
        montoesc=montoesc+'ES'
      endif
      montoesc=montoesc+' CON '
    endif
    montoesc=montoesc+substr(montonum,len(montonum)-1,2)+'/Cts.'
  endif
return montoesc
 
Saludos

Angel, Valencia, Venezuela

xH .997 - FW 7.9 - BCC55 - WorkShop - MySql
User avatar
ADBLANCO
 
Posts: 299
Joined: Mon Oct 22, 2007 3:03 pm
Location: Valencia - Venezuela

Re: Convertir números a palabras

Postby mmercado » Tue Oct 06, 2009 5:18 pm

Hola Rolando:
rolando wrote:Quisiera saber si existe alguna función que haga una conversión de números a frase de caractéres.

Mi rutina aNumLet con un pequeño ejemplo de muestra:
Code: Select all  Expand view
#include "FiveWin.ch"

Function Main()

   Local aLet, cNum := Space( 20 )

   While .T.
      If MsgGet( "Números a Letras", "Número:", @cNum )
         aLet := aNumLet( Val( cNum ), 100 )
         MsgInfo( aLet[ 1 ] + CRLF + aLet[ 2 ], "En Letras" )
         cNum := Space( 20 )
      Else
         Exit
      EndIf
   EndDo

Return Nil

//--------------------------------------------------------------//

Function aNumLet( nVar, nLong )

   Local aCent := { "CIENTO", "DOSCIENTOS", "TRESCIENTOS", "CUATROCIENTOS", "QUINIENTOS", "SEISCIENTOS", ;
                    "SETECIENTOS", "OCHOCIENTOS", "NOVECIENTOS" }, ;
         aUnid := { "UN", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE", "OCHO", "NUEVE" }, ;
         aDece := { "DIEZ", "ONCE", "DOCE", "TRECE", "CATORCE", "QUINCE", "DIECISEIS", "DIECISIETE", "DIECIOCHO", ;
                    "DIECINUEVE", "VEINTE", "VEINTIUN", "VEINTIDOS", "VEINTITRES", "VEINTICUATRO", "VEINTICINCO", ;
                    "VEINTISEIS", "VEINTISIETE", "VEINTIOCHO", "VEINTINUEVE" }, ;
         aDece1 := { "", "", "TREINTA", "CUARENTA", "CINCUENTA", "SESENTA", "SETENTA", "OCHENTA", "NOVENTA" }, ;
         cNumalf := Transform( nVar, "############.##" )

   Local aN2    := { Val( SubStr( cNumalf, 1, 3 ) ), Val( SubStr( cNumalf, 4, 3 ) ), Val( SubStr( cNumalf, 7, 3 ) ), ;
                     Val( SubStr( cNumalf, 10, 3 ) ), Val( SubStr( cNumalf, 14, 2 ) ) }, ;
         aN1    := {}, ;
         aRet   := {}, ;
         nV1    := 0, ;
         cLetra := "( " + If( Int( nVar ) == 0, "CERO ", "" )

   nLong := If( ValType( nLong ) = "N" .and. nLong > 0, nLong, 75 )

   For nV1 := 1 To 4
      aN1 := { Val( SubStr( Str( aN2[ nV1 ], 3 ), 1, 1 ) ), Val( SubStr( Str( aN2[ nV1 ], 3 ), 2, 1 ) ), ;
               Val( SubStr( Str( aN2[ nV1 ], 3 ), 3, 1 ) ), Val( SubStr( Str( aN2[ nV1 ], 3 ), 2, 2 ) ) }
      cLetra += If( aN1[ 1 ] > 0, If( aN1[ 1 ] == 1 .and. aN1[ 4 ] == 0, ;
                    "CIEN ", Trim( aCent[ aN1[ 1 ] ] ) + Space( 1 ) ), "" )
      cLetra += If( aN1[ 4 ] > 9 .and. aN1[ 4 ] < 30, Trim( aDece[ aN1[ 4 ] - 9 ] ) + Space( 1 ), "" )
      cLetra += If( aN1[ 4 ] > 29, aDece1[ aN1[ 2 ] ] + Space( 1 ) + If( aN1[ 3 ] > 0, "Y ", "" ), "" )
      cLetra += If( aN1[ 4 ] > 29 .or. aN1[ 4 ] < 10, If( aN1[ 3 ] > 0, ;
                    aUnid[ aN1[ 3 ] ] + Space( 1 ), "" ), "" )
      cLetra += If( nV1 = 2 .and. nVar > 999999.99, If( aN2[ nV1 ] != 1, "MILLONES", "MILLON" ) + ;
                    If( aN2[ 3 ] == 0 .and. aN2[ 4 ] = 0, " DE ", ", " ), "" )
      cLetra += If( ( nV1 = 1 .and. aN2[ nV1 ] > 0 ) .or. ( nV1 == 3 .and. aN2[ nV1 ] > 0 ), "MIL ", "" )
   Next

   cLetra += If( nVar > 1.99 .or. nVar < 1.00, "PESOS ", "PESO " ) + StrZero( aN2[ 5 ], 2 ) + "/100 M.N. )"
   cLetra := PadR( cLetra, nLong * 2 )
   aRet := { MemoLine( cLetra, nLong, 1 ), Memoline( cLetra, nLong, 2 ) }

Return aRet   /* regresa un arreglo con 2 elementos de longitud = nLong  */

Un abrazo.
manuelmercado at prodigy dot net dot mx
User avatar
mmercado
 
Posts: 782
Joined: Wed Dec 19, 2007 7:50 am
Location: Salamanca, Gto., México

Re: Convertir números a palabras

Postby MauroArevalo » Tue Oct 06, 2009 6:04 pm

Rolando:

Ahi va otra, la tome del foro y la modifique a mis necesidades...

Code: Select all  Expand view

***************
FUNCTION VALORES()

**TOTCTA ES LA VARIABLE CON LA CIFRA QUE TRAIGO PARA CONVERTIR A LETRAS

LOCAL TSTR   := STRZERO(TOTCTA,18,2)

LOCAL ACIFRA := {{'TRILLON','TRILLONES'},{'BILLON' ,'BILLONES'},;
                {'MILLON' ,'MILLONES'},{'MIL','MIL'},{'',''},{'CENT','CENTS'}}
LOCAL TX
LOCAL TEXTENSO :=''
LOCAL TSUBS    :=''
LOCAL TCENTAVOS:= VAL(SUBSTR(TSTR,17))

SDOTOT:=TOTCTA

IF (SDOTOT>0)
  IF (TCENTAVOS>0)
    TEXTENSO:=EXTCEM(STRZERO(TCENTAVOS,3))+ACIFRA[6][IIF(TCENTAVOS=1,1,2)]
  ENDIF
  IF (INT(SDOTOT)>0)
    TEXTENSO:=IIF(INT(SDOTOT)=1,'PESO MCTE','PESOS MCTE')+IIF(TCENTAVOS>0,' Y ','')+TEXTENSO
  ENDIF
  FOR TX := 5 TO 1 STEP -1
    IF (VAL(TSUBS:=SUBSTR(TSTR,(TX*3)-2,3))>0)
      TEXTENSO:=ALLTRIM(EXTCEM(TSUBS)+ACIFRA[TX][IIF(VAL(TSUBS)=1,1,2)])+" "+TEXTENSO
    ENDIF
  NEXT TX
ENDIF

LETNUM:=CAMT:=TEXTENSO
BLANCO=SPAC(CTR+2)
ANCHO=LEN(CAMT)
LIN1:=""
Posblanco:=0
TAMANO:=LEN(CAMT)

NUEVO:=""
TAM:=LEN(CAMT)

FOR I := 1 TO TAM
  EXTRAE:=SUBS(LETNUM,I,1)
  IF EXTRAE=" "
    PosBlanco:=I
  ENDIF
  IF I=CTR   //CTR Es el ancho de linea dependiendo la letra DRAF= 52  y CONDENSANDA = 84
   I:=TAM
   LIN1:=SUBS(LETNUM,1,PosBlanco)
   LIN2:=SUBS(LETNUM,(PosBlanco+1),(TAMANO-PosBlanco))
  ELSE
   LIN1:=LIN1+SUBS(LETNUM,I,1)
  ENDIF
NEXT

AUM=CTR/2    //Divide en dos la linea..

IF TAM<=CTR                  //  Cuando sale una linea....
  Agrega:=(CTR-LEN(LIN1))/2
  LIN1:=LIN1+REPL("*.",Agrega)
  LIN2=REPL("*.",AUM)
 ELSE
  Agrega:=(CTR-LEN(LIN1))    //Cuando salen dos lineas..
  FOR I := 1 TO CTR
    EXTRAE:=SUBS(LIN1,I,1)
   IF EXTRAE=" "
     NUEVO:=NUEVO+SPAC(2)
    ELSE
     NUEVO:=NUEVO+SUBS(LIN1,I,1)
   ENDIF
  NEXT
  LIN1:=ALLTRIM(NUEVO)
  Agrega:=((LEN(LIN1)-LEN(LIN2))/2)
  LIN2:=LIN2+REPL("*.",Agrega)
ENDIF


***************************
FUNCTION EXTCEM(ACEM)

LOCAL AVAL := {VAL(SUBSTR(ACEM,1,1)),VAL(SUBSTR(ACEM,2,1)),VAL(SUBSTR(ACEM,3,1))}
LOCAL TTAM,TEXT := ''
LOCAL ACENT := {'CIENTO','DOSCIENTOS','TRESCIENTOS','CUATROCIENTOS','QUINIENTOS','SEISCIENTOS','SETECIENTOS','OCHOCIENTOS','NOVECIENTOS'}
LOCAL ADECE := {' DIEZ',' VEINTE',' TREINTA',' CUARENTA',' CINCUENTA',' SESENTA',' SETENTA',' OCHENTA',' NOVENTA' }
LOCAL AVINT := {'ONCE','DOCE','TRECE','CATORCE','QUINCE','DIECISEIS','DIECISIETE','DIECIOCHO','DIECINUEVE' }
LOCAL AUNID := {'UN','DOS','TRES','CUATRO','CINCO','SEIS','SIETE','OCHO','NUEVE' }

IF (VAL(ACEM)>0)
  IF (VAL(ACEM)==100)
     TEXT := 'CIEN '
   ELSE
    IF (AVAL[1]>0)
      TEXT := ALLTRIM(ACENT[AVAL[1]]+IIF((AVAL[2]+AVAL[3]>0),' ',' '))
    ENDIF
    IF (AVAL[2]=1).AND.(AVAL[3]>0)
      TEXT := TEXT+' '+AVINT[AVAL[3]]
     ELSE
      IF (AVAL[2]>0)                               // Y
        TEXT := ALLTRIM((TEXT+''+ADECE[AVAL[2]]+IIF(AVAL[3]>0,' Y','')))
      ENDIF
      IF(AVAL[3])>0
        TEXT :=TEXT+IIF(AVAL[3]>0,' '+AUNID[AVAL[3]],' ')
      ENDIF
    ENDIF
  ENDIF
ENDIF
RETURN(TEXT := TEXT+' ')

 
Edgar Mauricio Arévalo Mogollón.
Bogotá DC. Colombia
FWH FTDN, xHarbour 1.2.1, Pelles C, Fivedit, Visual Studio Code, Borland 7.30, Mysql, Dbfs
http://www.hymplus.com http://www.hymlyma.com
Tratando de retomar la programación....
User avatar
MauroArevalo
 
Posts: 105
Joined: Thu Jan 19, 2006 11:47 pm
Location: Bogota DC. Colombia

Re: Convertir números a palabras

Postby rolando » Tue Oct 06, 2009 6:25 pm

Muchísimas gracias a todos.

Ya tengo para trabajar.

Saludos.

Rolando :D
User avatar
rolando
 
Posts: 593
Joined: Sat May 12, 2007 11:47 am
Location: San Nicolás - BA - ARGENTINA

Re: Convertir números a palabras

Postby acuellar » Tue Oct 06, 2009 7:09 pm

Rolando aqui tene otra forma:

Para llamar la función es asi:
Literal(Cantidad,"dólares",1) //1 por si llevará centavos Ej: 45/100
Code: Select all  Expand view

FUNCTION Literal( nNro,Moneda,Centavos )
       Private aCen:= { "CIENTO", "DOSCIENTOS", "TRESCIENTOS", "CUATROCIENTOS","QUniNIENTOS", "SEISCIENTOS", "SETECIENTOS", "OCHOCIENTOS", "NOVECIENTOS", "MIL" }
       Private aDec:= { "DIEZ", "VEINTI", "TREINTA", "CUARENTA", "CINCUENTA","SESENTA", "SETENTA", "OCHENTA", "NOVENTA", "CIEN" }
       Private aUni:= { "UN", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE","OCHO", "NUEVE", "DIEZ", "ONCE", "DOCE", "TRECE", "CATORCE", ;
                        "QUniNCE","DIECISEIS","DIECISIETE","DIECIOCHO","DIECINUEVE","VEINTE" }
       Private nL, nD, nML, nMI, nMC, lite:= ""
       nL:=STR( nNro, 13, 3 )
       nD:=Subs(nL,11,2)
       nML:=VAL( SUBSTR( nL, 1, 3 ) )
       nMI:=VAL( SUBSTR( nL, 4, 3 ) )
       nMC:=VAL( SUBSTR( nL, 7, 3 ) )
       IF nML > 0
          ProLiteral( nML )
          IF nML > 1
             Lite+= "MILLONES "
          ELSE
             Lite+= "MILLON "
          ENDIF
       ENDif
       IF nMI > 0
          ProLiteral( nMI )
          Lite+= "MIL "
       ENDIF
       IF nMC > 0
          ProLiteral( nMC )
       ENDIF
      IF Centavos=1
         Lite+= nD + "/100 "
      ENDI
      Lite+=Moneda
     
RETURN lite
*
FUNCTION ProLiteral( nLit )
       Local CN:= VAL( SUBSTR( STR( nLit, 3, 0 ), 1, 1 ) )
       Local Cen:= VAL( SUBSTR( STR( nLit, 3, 0 ), 2, 2 ) )
       Local Dec:= VAL( SUBSTR( STR( nLit, 3, 0 ), 2, 1 ) )
       Local Uni:= VAL( SUBSTR( STR( nLit, 3, 0 ), 3, 1 ) )

       IF nLit > 100
          Lite+= aCen[ cn ] + " "
       ELSE
          IF nLit = 100
             Lite+= aDEC[10] + ' '
          ENDIF
       ENDif
       IF Cen < 21
          Uni:=Cen
       ELSE
          Lite+= aDEC[ Dec ]
          IF Dec > 2
             Lite+= " "
             IF Uni > 0
                Lite += "Y "
             ENDIF
          ENDIF
       ENDIF
       IF Uni > 0
          Lite += aUNI[ Uni ] + " "
       ENDIF
Return Nil
 

Espero te sirva

Saludos

Adhemar
Saludos,

Adhemar C.
User avatar
acuellar
 
Posts: 1594
Joined: Tue Oct 28, 2008 6:26 pm
Location: Santa Cruz-Bolivia

Re: Convertir números a palabras

Postby rolando » Tue Oct 06, 2009 10:13 pm

Adhemar,

Muchas gracias a tí también.

Con toda la ayuda recibida, cuestión solucionada.

Suerte.

Rolando :D
User avatar
rolando
 
Posts: 593
Joined: Sat May 12, 2007 11:47 am
Location: San Nicolás - BA - ARGENTINA

Re: Convertir números a palabras

Postby Willi Quintana » Wed Oct 07, 2009 1:15 am

Aqui va otra:
viewtopic.php?f=17&t=10175

Salu2
User avatar
Willi Quintana
 
Posts: 1003
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú

Re: Convertir números a palabras

Postby Dioni » Wed Oct 07, 2009 4:47 am

Hola Rolando.

Code: Select all  Expand view
#include "fivewin.ch"

xnume:=3200.12

?letras(xnume)+' nuevos soles'
?letras(xnume)+' dolares americanos'
retu nil

PROCEDURE LETRAS ( NUM )
 NIV:= {[MIL], [MILLON], [BILLON], [TRILLON] };   LET:= Space(40)
 CAD := Alltrim( Str(INT(NUM)) );CER := IF (Len(CAD)%3 < 3 .AND. Len(CAD)%3 <> 0, 3-(Len(CAD)%3), 0)
 CAD := Replicate ( '0', CER ) + CAD;LON := Len(CAD)
 LETRA:=''
 FOR I:=1 TO Int(Len(CAD)/3)
     numero:= Val( SubStr(CAD, LON-2, 3) ); NIVEL:=''
     NUMLET ( numero, @LET )
     LET   := IF (I>1, IF (numero=1, Left(LET,2), LET), LET)
      LON   := LON - 3
      IF I < Int(Len(CAD)/3)
          NIVEL := NIV[I]
          IF I>1
             nums:= Val ( SubStr(CAD, LON-2, 3) )
             NIVEL:= NIVEL + IF(nums>1, 'ES', '')
          ENDIF
      ENDIF   
     LETRA:=  NIVEL + ' ' +  LET  +  ' '+LETRA
 NEXT
 LETRA:= Alltrim(LETRA) + ' CON ' + Strzero( (NUM-INT(NUM))*100, 2 ) + '/100 '
RETURN LETRA

PROCEDURE NUMLET ( num, let )
 numc:= nomd:= nomu:= ''
 C:= {[CIEN], [DOSCIENTOS], [TRESCIENTOS], [CUATROCIENTOS], [QUINIENTOS], [SEISCIENTOS], [SETECIENTOS], [OCHOCIENTOS], [NOVECIENTOS]}
 D:= {[DIEZ], [VEINTE]    , [TREINTA]    , [CUARENTA]     , [CINCUENTA] , [SESENTA]    , [SETENTA]    , [OCHENTA]    , [NOVENTA]}      
 U:= {[UNO] , [DOS]       , [TRES]       , [CUATRO]       , [CINCO]     , [SEIS]       , [SIETE]      , [OCHO]       , [NUEVE]}
 X:= {[ONCE], [DOCE]      , [TRECE]      , [CATORCE]      , [QUINCE]    , [DIECISEIS]  , [DIECISIETE] , [DIECIOCHO]  , [DIECINUEVE]}
 cen := INT (num/100);    dec := INT ((num%100)/10);  uni := ( (num%100) % 10 )
 TO  := IF (cen=1, IF (dec>0 .OR. uni>0, 'TO', '') , '')  
 nomc := IF(cen>0, C[cen] + TO + [ ] ,'')                
 IF dec=1 .AND. uni>=1                                  
    nomu := IF(uni>0, X[uni], '' )                        
 ELSE                                                    
    nomd := IF(dec>0, D[dec] ,'')                          
     IF dec=2 .AND. uni>0                                  
        nomd:= Left(nomd,5) + 'I'
     ELSEIF dec>=3  .AND. uni>0
        nomd:= nomd + ' Y '
     ENDIF
    nomu := IF(uni>0, U[uni]+[ ] ,'')
 ENDIF
 let := nomc + nomd + nomu ;let := Alltrim( let )
RETURN


desde Lima-Perú Dionicio G. Chura Quispe.
Dioni
 
Posts: 36
Joined: Tue May 12, 2009 8:45 pm
Location: Lima - Perú

Re: Convertir números a palabras

Postby rolando » Wed Oct 07, 2009 2:08 pm

Buen día,

De nuevo, muchas gracias a todos.

Comento que utilicé la función que posteara Adhemar, haciendole pequeñas modificaciones para que se adaptase al uso que voy a darle.

La llamo así:

Code: Select all  Expand view
NumToCadena(nCantidad,cMoneda,lDecimales)


- nCantidad es el número a convertir
- cMoneda el tipo de moneda (ej. "pesos")
- lDecimales es un valor lógico (.t. si se quiere mostrar los decimales y .f. si no se quiere)

La cuestión es que me sirve para mostrar valores de moneda y, con mi pequeña modificación, para mostrar valores sin moneda. Además los decimales o centavos, los muestra también como cadena.

Si hago:

Code: Select all  Expand view
NumToCadena(nCantidad,,lDecimales)

Directamente me devuelve los numeros con o sin decimales según sea el valor de lDecimales pero sin el tipo de moneda.

Esto último sirve para mostrar fechas al comienzo de las notas convirtiéndolas a cadena.

Pego la función de Adhemar con mis modificaciones:

Code: Select all  Expand view
FUNCTION NumToCadena(nNro,cMoneda,lDecimales)

             Private aCen:= { "ciento", "doscientos", "trescientos", "cuatrocientos","quinientos",;
                             "seiscientos", "setecientos", "ochocientos", "novecientos", "mil" }
       Private aDec:= { "diez", "veinti", "treinta", "cuarenta", "cincuenta","sesenta", "setenta", ;
                              "ochenta", "noventa", "cien" }
       Private aUni:= { "un", "dos", "tres", "cuatro", "cinco", "seis", "siete","ocho", "nueve", ;
                              "diez", "once", "doce", "trece", "catorce", "quince","dieciseis","diecisiete",;
                                                "dieciocho","diecinueve","veinte" }
       Private nL, nD, nML, nMI, nMC, lite:= ""
             default cMoneda:="niente"
             nL:=STR( nNro, 13, 3 )
       nD:=Subs(nL,11,2)
       nML:=VAL( SUBSTR( nL, 1, 3 ) )
       nMI:=VAL( SUBSTR( nL, 4, 3 ) )
       nMC:=VAL( SUBSTR( nL, 7, 3 ) )
       IF nML > 0
          ProLiteral( nML )
          IF nML > 1
             Lite+= "millones "
          ELSE
             Lite+= "millón "
          ENDIF
       ENDif
       IF nMI > 0
          ProLiteral( nMI )
          Lite+= "mil "
       ENDIF
       IF nMC > 0
          ProLiteral( nMC )
       ENDIF
      IF lDecimales .and. cMoneda <> "niente"
         Lite+= cMoneda + " con " + Centavos(nD) + " centavos"
                 RETURN lite
            ENDIF
      IF lDecimales .and. cMoneda = "niente"
         Lite+= " con " + Centavos(nD)
                 RETURN lite
            ENDIF

            if cMoneda <> "niente"
              Lite+=cMoneda
            endif

RETURN lite

static FUNCTION ProLiteral( nLit )
       Local CN:= VAL( SUBSTR( STR( nLit, 3, 0 ), 1, 1 ) )
       Local Cen:= VAL( SUBSTR( STR( nLit, 3, 0 ), 2, 2 ) )
       Local Dec:= VAL( SUBSTR( STR( nLit, 3, 0 ), 2, 1 ) )
       Local Uni:= VAL( SUBSTR( STR( nLit, 3, 0 ), 3, 1 ) )
       IF nLit > 100
          Lite+= aCen[ cn ] + " "
       ELSE
          IF nLit = 100
             Lite+= aDEC[10] + ' '
          ENDIF
       ENDif
       IF Cen < 21
          Uni:=Cen
       ELSE
          Lite+= aDEC[ Dec ]
          IF Dec > 2
             Lite+= " "
             IF Uni > 0
                Lite += "y "
             ENDIF
          ENDIF
       ENDIF
       IF Uni > 0
          Lite += aUNI[ Uni ] + " "
       ENDIF
Return Nil


//-----------------------------------------------

Static Function Centavos(nD)
     local cCentavos:="" , nDec , nUni , nDe
             Local aDec:= { "diez", "veinti", "treinta", "cuarenta", "cincuenta","sesenta",;
                           "setenta", "ochenta", "noventa", "cien" }
       Local aUni:= { "un", "dos", "tres", "cuatro", "cinco", "seis", "siete","ocho", ;
                           "nueve", "diez", "once", "doce", "trece", "catorce", "quince", ;
                                         "dieciseis","diecisiete","dieciocho","diecinueve","veinte" }

     nDn :=val(substr(nD,1,2))
     nDec:=val(substr(nD,1,1))
     nUni:=val(substr(nD,2,1))

     if nDn = 0
        cCentavos:="cero"
        Return cCentavos
     endif

     if nDn > 20
        cCentavos:=aDec[nDec]
            if nDec = 2      // se va a usar "veinti"
                cCentavos:=cCentavos+aUni[nUni]
             else
                cCentavos:=cCentavos+" y "+aUni[nUni]
            endif
        else
           cCentavos:=aUni[nDn]
     endif
Return cCentavos

//----------------------------------------------------------------------------------------------------------------


Saludos.

Rolando :D
User avatar
rolando
 
Posts: 593
Joined: Sat May 12, 2007 11:47 am
Location: San Nicolás - BA - ARGENTINA


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 43 guests