Files
Lanterna_H/numeroATexto.bas

135 lines
3.9 KiB
QBasic

B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.8
@EndOfDesignText@
Sub Class_Globals
Dim UnitWords() As String = Array As String( _
"", "un", "dos", "tres", "cuatro", _
"cinco", "seis", "siete", "ocho", "nueve", _
"diez", "once", "doce", "trece", "catorce", _
"quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve" _
)
Dim TenWords() As String = Array As String( _
"", "diez", "veinte", "treinta", "cuarenta", _
"cincuenta", "sesenta", "setenta", "ochenta", "noventa" _
)
'only need to go up to Pentillions to handle largest Long integer, but while we're here...
Dim ThousandWords() As String = Array As String( _
"", "mil", "millon", "billon", "trillon", _
"Cuadrillon", "Pentillion", "Sexillion", "Septillion", "Octillion" _
)
Dim moneda As String = "pesos"
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
Return Me
End Sub
Sub NumberToWords(N As Double) As String
If N < 0 Then
Return "Minus " & NumberToWordsPositive(-N)
Else
Return NumberToWordsPositive(N) 'including zero
End If
End Sub
Sub NumberToWordsPositive(N0 As Double) As String
Private N As Long
Private temp1() As String = Regex.Split("\.", NumberFormat2(N0, 1, 2, 2, False))
Private conDecimales As Boolean = False
Private losCents As String = 0
' Log(N0)
' Log(temp1.Length)
' Log(temp1(0))
If temp1.Length > 1 Then
conDecimales = True
N = temp1(0)
losCents = temp1(1)
' Log($"Con Decimales: ${losCents}"$)
Else
N = N0
End If
' Log(">> " & N)
If N = 0 Then
Return "Cero" 'that gets rid of that pesky special case
End If
Dim GroupsOfThree(10) As Int
Dim NumGroupsOfThree As Int = 0
Do While N <> 0
GroupsOfThree(NumGroupsOfThree) = N Mod 1000
NumGroupsOfThree = NumGroupsOfThree + 1
N = N / 1000
Loop
Dim Temp As String = ""
For GroupOfThree = NumGroupsOfThree - 1 To 0 Step -1
Dim ThisGroup As Int = GroupsOfThree(GroupOfThree)
If ThisGroup <> 0 Then
If Temp.Length <> 0 Then
' If GroupOfThree = 0 And ThisGroup < 100 Then
' Temp = Temp & " y "
'' If Temp.Contains("mil y ") Then Temp = "mil "
' Else
' Temp = Temp & " "
' End If
Temp = Temp & " "
End If
Temp = Temp & NumberToWords1000(ThisGroup)
If GroupOfThree <> 0 Then
' Log($"${Temp} - ${ThousandWords(GroupOfThree)}"$)
Temp = Temp & " " & ThousandWords(GroupOfThree)
If Temp = "un mil" Then
' Log(9)
Temp = "mil"
End If
End If
End If
Next
Temp = Temp.Substring2(0,1).ToUppercase & Temp.SubString(1)
Return Temp & $" ${moneda} ${NumberFormat2(losCents, 2, 0, 0, False)}/100 M.N."$
End Sub
Sub NumberToWords1000(N As Int) As String
If N < 100 Then
' Log(1)
Return NumberToWords100(N)
End If
Dim Hundreds As String = UnitWords(N / 100) & "cientos" 'Hundreds always non-blank since N < 100 already done
If UnitWords(N/100) = "nueve" Then Hundreds = "novecientos"
If UnitWords(N/100) = "cinco" Then Hundreds = "quinientos"
If UnitWords(N/100) = "siete" Then Hundreds = "setecientos"
' Log($"${N/100} - ${UnitWords(N / 100)}"$)
If UnitWords(N / 100) = "un" Then Hundreds = "ciento"
Dim TensUnits As String = NumberToWords100(N Mod 100) 'TensUnits could be blank if digits are 00
If TensUnits.Length = 0 Then
' Log(2)
If UnitWords(N / 100) = "un" Then Hundreds = "cien"
Return Hundreds
Else
' Log(3)
Return Hundreds & " " & TensUnits
End If
End Sub
Sub NumberToWords100(N As Int) As String
If N < 20 Then
Return UnitWords(N)
End If
Dim Tens As String = TenWords(N / 10) 'Tens always non-blank since N < 20 already done
Dim Units As String = UnitWords(N Mod 10) 'Units could be blank if digit is 0
If Units.Length = 0 Then
Return Tens
Else
' Log($"${Tens} - ${Units}"$)
If Tens = "veinte" Then
Return "veinti" & Units
Else
Return Tens & " y " & Units
End If
End If
End Sub