casamoreno
New Member
- Joined
- Jun 16, 2005
- Messages
- 5
For those looking for spanish mexican pesos numbers to text conversion, this is my adaptation from original code. Please comment as needed.
Option Explicit
'Main Function
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " MIL "
Place(3) = " MILLONES "
Place(4) = " Billion " ' not really needed
Place(5) = " Trillion " ' not really needed
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = (Left(Mid(MyNumber, DecimalPlace + 1) & _
"00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
Dollars = "CERO PESOS"
Case "UN"
Dollars = "UN PESO"
Case Else
Dollars = Dollars & " PESOS"
End Select
Select Case Cents
Case ""
Cents = " Y 00/100 M.N."
Case "UN"
Cents = " Y 01/100 M.N."
Case Else
Cents = " Y " & Cents & "/100 M.N."
End Select
SpellNumber = Dollars & Cents
End Function
' Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Val(MyNumber) = 100 Then
Result = "CIEN "
ElseIf Mid(MyNumber, 1, 1) <> "0" Then
Select Case Mid(MyNumber, 1, 1)
Case 1: Result = "CIENTO "
Case 2: Result = "DOCIENTOS "
Case 3: Result = "TRECIENTOS "
Case 4: Result = "CUATROCIENTOS "
Case 5: Result = "QUINIENTOS "
Case 6: Result = "SEICIENTOS "
Case 7: Result = "SETECIENTOS "
Case 8: Result = "OCHOCIENTOS "
Case 9: Result = "NOVECIENTOS "
Case Else
End Select
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
' Converts a number from 10 to 99 into text.
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "DIEZ"
Case 11: Result = "ONCE"
Case 12: Result = "DOCE"
Case 13: Result = "TRECE"
Case 14: Result = "CATORCE"
Case 15: Result = "QUINCE"
Case 16: Result = "DIECISEIS"
Case 17: Result = "DIECISIETE"
Case 18: Result = "DIECIOCHO"
Case 19: Result = "DIECINUEVE"
Case Else
End Select
ElseIf Val(Left(TensText, 1)) = 2 Then ' If value between 20-29...
Select Case Val(TensText)
Case 20: Result = "VEINTE"
Case 21: Result = "VEINTIUN"
Case 22: Result = "VEINTIDOS"
Case 23: Result = "VEINTITRES"
Case 24: Result = "VEINTICUATRO"
Case 25: Result = "VEINTICINCO"
Case 26: Result = "VEINTISEIS"
Case 27: Result = "VEINTISIETE"
Case 28: Result = "VEINTIOCHO"
Case 29: Result = "VEINTINUEVE"
Case Else
End Select
Else
Select Case Val(Left(TensText, 1))
Case 3: Result = "TREINTA "
Case 4: Result = "CUARENTA "
Case 5: Result = "CINCUENTA "
Case 6: Result = "SESENTA "
Case 7: Result = "SETENTA "
Case 8: Result = "OCHENTA "
Case 9: Result = "NOVENTA "
Case Else
End Select
Result = Result & "Y " & GetDigit _
(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
' Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "UN"
Case 2: GetDigit = "DOS"
Case 3: GetDigit = "TRES"
Case 4: GetDigit = "CUATRO"
Case 5: GetDigit = "CINCO"
Case 6: GetDigit = "SEIS"
Case 7: GetDigit = "SIETE"
Case 8: GetDigit = "OCHO"
Case 9: GetDigit = "NUEVE"
Case Else: GetDigit = ""
End Select
End Function
Option Explicit
'Main Function
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " MIL "
Place(3) = " MILLONES "
Place(4) = " Billion " ' not really needed
Place(5) = " Trillion " ' not really needed
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = (Left(Mid(MyNumber, DecimalPlace + 1) & _
"00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
Dollars = "CERO PESOS"
Case "UN"
Dollars = "UN PESO"
Case Else
Dollars = Dollars & " PESOS"
End Select
Select Case Cents
Case ""
Cents = " Y 00/100 M.N."
Case "UN"
Cents = " Y 01/100 M.N."
Case Else
Cents = " Y " & Cents & "/100 M.N."
End Select
SpellNumber = Dollars & Cents
End Function
' Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Val(MyNumber) = 100 Then
Result = "CIEN "
ElseIf Mid(MyNumber, 1, 1) <> "0" Then
Select Case Mid(MyNumber, 1, 1)
Case 1: Result = "CIENTO "
Case 2: Result = "DOCIENTOS "
Case 3: Result = "TRECIENTOS "
Case 4: Result = "CUATROCIENTOS "
Case 5: Result = "QUINIENTOS "
Case 6: Result = "SEICIENTOS "
Case 7: Result = "SETECIENTOS "
Case 8: Result = "OCHOCIENTOS "
Case 9: Result = "NOVECIENTOS "
Case Else
End Select
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
' Converts a number from 10 to 99 into text.
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "DIEZ"
Case 11: Result = "ONCE"
Case 12: Result = "DOCE"
Case 13: Result = "TRECE"
Case 14: Result = "CATORCE"
Case 15: Result = "QUINCE"
Case 16: Result = "DIECISEIS"
Case 17: Result = "DIECISIETE"
Case 18: Result = "DIECIOCHO"
Case 19: Result = "DIECINUEVE"
Case Else
End Select
ElseIf Val(Left(TensText, 1)) = 2 Then ' If value between 20-29...
Select Case Val(TensText)
Case 20: Result = "VEINTE"
Case 21: Result = "VEINTIUN"
Case 22: Result = "VEINTIDOS"
Case 23: Result = "VEINTITRES"
Case 24: Result = "VEINTICUATRO"
Case 25: Result = "VEINTICINCO"
Case 26: Result = "VEINTISEIS"
Case 27: Result = "VEINTISIETE"
Case 28: Result = "VEINTIOCHO"
Case 29: Result = "VEINTINUEVE"
Case Else
End Select
Else
Select Case Val(Left(TensText, 1))
Case 3: Result = "TREINTA "
Case 4: Result = "CUARENTA "
Case 5: Result = "CINCUENTA "
Case 6: Result = "SESENTA "
Case 7: Result = "SETENTA "
Case 8: Result = "OCHENTA "
Case 9: Result = "NOVENTA "
Case Else
End Select
Result = Result & "Y " & GetDigit _
(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
' Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "UN"
Case 2: GetDigit = "DOS"
Case 3: GetDigit = "TRES"
Case 4: GetDigit = "CUATRO"
Case 5: GetDigit = "CINCO"
Case 6: GetDigit = "SEIS"
Case 7: GetDigit = "SIETE"
Case 8: GetDigit = "OCHO"
Case 9: GetDigit = "NUEVE"
Case Else: GetDigit = ""
End Select
End Function