De números a Letras

Por Carlos Maldonado (México)
© Copyrights 1998 by FoxPress, All rights reserved
FoxPress, Abril 1998

Para probar esta utilidad basta con que escribáis

SET PROCEDURE TO [PATH\]convert.prg
? Número(numero)
Espero que os sirva
*** Programa para evaluar un número y convertirlo a texto

function numero(num)

Local S_Billo
local S_MilMill
local S_Mill
local S_Mil
local S_Cien
local S_Cents
Local No_Hay_Mil
Local Tex_Num
Local Str_Num

S_Billo = ""
S_MilMill = ""
S_Mill = ""
S_Mil = ""
S_Cien = ""
S_Cents = ""
No_Hay_Mil = .t.
Tex_Num = ""
Str_Num = ""


str_Num =alltrim(transform(abs(num),"999999999999999.99"))

do while int(len(str_num)/3)!=len(str_num)/3
	str_num = "0" + str_num
enddo

if Val(str_num) < 1
 tex_num = " Cero"
endif

if len(Str_num)> 18 .or. Str_Num = "***"
 tex_num = "*****"
 return tex_num
endif

* Billones //
if len(str_num) = 18
 s_billo = substr(str_num,1,3)
 tex_num = tex_num + det_qty(@s_billo)
 tex_num = tex_num + IIF( substr(str_num,1,3)="001" .and. len(tex_num) <= 4, ;
           " Billon", " Billones")
 str_num = substr(str_num,4,15)
endif

* Miles de Millones //
if len(str_num) = 15
 s_MilMill = substr(str_num,1,3)
 se_pone = IIF(val(s_milmill)>0,.t.,.f.)
 tex_num = tex_num + det_qty(@s_milmill)
 if se_pone
	tex_num = tex_num + " Mil"
 endif
 str_num = substr(str_num,4,15)
endif


* Millones //
if len(str_num) = 12
	s_Mill = substr(str_num,1,3)
	se_pone = IIF(val(s_mill)>0,.t.,.f.)
	tex_num = tex_num + det_qty(@s_Mill)
	tex_num = tex_num + IIF( substr(str_num,1,3)="001" and not "Mil"$tex_num," Millon",IIF(val(S_MilMill)>0 .or. se_pone," Millones",""))
	str_num = substr(str_num,4,9)
endif


* Miles //
if len(str_num) = 9
	s_Mil = substr(str_num,1,3)
	no_hay_mil  = IIF(Val(s_mil)>0,.f.,.t.)
	tex_num = tex_num + det_qty(@s_Mil)
	tex_num = tex_num + IIF(substr(str_num,1,3)>"000", " Mil", "")
	str_num = substr(str_num,4,6)
endif

* Cienes //
if len(str_num) = 6
	s_cien = substr(str_num,1,3)
	peso=IIF(Val(S_cien)= 1 AND no_hay_mil,.t.,.f.)
	tex_num = tex_num + det_qty(@s_cien)
	str_num = substr(str_num,4,3)
endif

if " Un "==left(Tex_num,4)
	Tex_Num=substr(Tex_Num,4)
endif

return(ltrim(tex_num))

**
** Función  Det_qty(n)
** Determina cantidad.


Function Det_qty(n)

local Ct
local Xc
local Dc
local tx
Declare Ct[9]
Declare Xc[29]
Declare Dc[9]

Ct[1] = " Ciento"
Ct[2] = " Doscientos"
Ct[3] = " Trescientos"
Ct[4] = " Cuatrocientos"
Ct[5] = " Quinientos"
Ct[6] = " Seiscientos"
Ct[7] = " Setecientos"
Ct[8] = " Ochocientos"
Ct[9] = " Novecientos"

Xc[1] = " Un"
Xc[2] = " Dos"
Xc[3] = " Tres"
Xc[4] = " Cuatro"
Xc[5] = " Cinco"
Xc[6] = " Seis"
Xc[7] = " Siete"
Xc[8] = " Ocho"
Xc[9] = " Nueve"
Xc[10]= " Diez"
Xc[11]= " Once"
Xc[12]= " Doce"
Xc[13]= " Trece"
Xc[14]= " Catorce"
Xc[15]= " Quince"
Xc[16]= " Dieciseis"
Xc[17]= " Diecisiete"
Xc[18]= " Dieciocho"
Xc[19]= " Diecinueve"
Xc[20]= " Veinte"
Xc[21]= " Veintiun"
Xc[22]= " Veintidos"
Xc[23]= " Veintitres"
Xc[24]= " Veinticuatro"
Xc[25]= " Veinticinco"
Xc[26]= " Veintiseis"
Xc[27]= " Veintisiete"
Xc[28]= " Veintiocho"
Xc[29]= " Veintinueve"

Dc[1] = ""
Dc[2] = ""
Dc[3] = " Treinta"
Dc[4] = " Cuarenta"
Dc[5] = " Cincuenta"
Dc[6] = " Sesenta"
Dc[7] = " Setenta"
Dc[8] = " Ochenta"
Dc[9] = " Noventa"

tx= ""
if n = "100"
 return " Cien"
endif

tx = tx + IIF(val(substr(n,1,1))>0, Ct[val(substr(n,1,1))], "")
tx = tx + IIF(val(substr(n,2,2))>0 and val(substr(n,2,2))<30,Xc[val(substr(n,2,2))], "")
tx = tx + IIF(val(substr(n,2,2))>29 , Dc[val(substr(n,2,1))]+ IIF(val(substr(n,3,1))>0," y"+Xc[val(substr(n,3,1))],""), "")
return(tx)