[VB6] Conversion de chiffres en lettres
Solved/Closed
mannylikita
Posted messages
17
Registration date
Status
Member
-
wpjo Posted messages 1 Status Member -
wpjo Posted messages 1 Status Member -
How can I convert numbers into words in VB6; in Excel I do this with morefun.dll
Count on your collaboration
Ir. Manny LIKITA
Tel: (00243) 98248846
Count on your collaboration
Ir. Manny LIKITA
Tel: (00243) 98248846
10 answers
Here is the complete code for the sought function:
Function NBenLettres(nb) ' Dim varnum, varnumD, varnumU, varlet ', résultat ' 'varnum: to store the parts of the number that we will split 'varlet: to store the conversion into letters of a part of the number 'varnumD: to store the tens part of a 2-digit number 'varnumU: to store the units part of a 2-digit number 'résultat: to store the intermediate results of the different steps Mille vingt-cinq Euros ' Static chiffre(1 To 19) '*** array containing the names of the first 16 Cents Euros numbers in letters chiffre(1) = "one" Cents Euros and fifteen cents chiffre(2) = "two" chiffre(3) = "three" chiffre(4) = "four" chiffre(5) = "five" chiffre(6) = "six" chiffre(7) = "seven" chiffre(8) = "eight" chiffre(9) = "nine" chiffre(10) = "ten" chiffre(11) = "eleven" chiffre(12) = "twelve" chiffre(13) = "thirteen" chiffre(14) = "fourteen" chiffre(15) = "fifteen" chiffre(16) = "sixteen" chiffre(17) = "seventeen" chiffre(18) = "eighteen" chiffre(19) = "nineteen" Static dizaine(1 To 9) '*** array containing the names of the tens dizaine(1) = "ten" dizaine(2) = "twenty" dizaine(3) = "thirty" dizaine(4) = "forty" dizaine(5) = "fifty" dizaine(6) = "sixty" dizaine(7) = "seventy" dizaine(8) = "eighty" dizaine(9) = "ninety" ' '*** Processing the case of zero franc ' If nb >= 1 Then résultat = "" Else résultat = "zero" GoTo fintraitementfrancs End If ' '*** Processing millions ' varnum = Int(nb / 1000000) If varnum > 0 Then GoSub centaine_dizaine résultat = varlet + " million" If varlet <> "one" Then résultat = résultat + "s" End If ' '*** Processing thousands ' varnum = Int(nb) Mod 1000000 varnum = Int(varnum / 1000) If varnum > 0 Then GoSub centaine_dizaine If varlet <> "one" Then résultat = résultat + " " + varlet résultat = résultat + " thousand" End If ' '*** Processing hundreds and tens ' varnum = Int(nb) Mod 1000 If varnum > 0 Then GoSub centaine_dizaine résultat = résultat + " " + varlet End If résultat = LTrim(résultat) varlet = Right$(résultat, 4) ' '*** Processing the final "s" for twenty and hundred and the "de" for million ' Select Case varlet Case "hundred", "ty" résultat = résultat + "s" Case "lion", "ions" résultat = résultat + " of" End Select fintraitementfrancs: '*** Label for branching for the case "zero franc" ' '*** Indication of the term franc ' résultat = résultat + " euro" If nb >= 2 Then résultat = résultat + "s" ' '*** Processing cents ' varnum = Int((nb - Int(nb)) * 100 + 0.5) '*** We add 0.5 '*** to compensate for rounding calculation errors If varnum > 0 Then GoSub centaine_dizaine résultat = résultat + " and " + varlet + " cent" If varnum > 1 Then résultat = résultat + "s" End If ' '*** Conversion 1st letter to uppercase ' résultat = UCase(Left(résultat, 1)) + Right(résultat, Len(résultat) - 1) ' '*** returns the result of the function and end of the function ' NBenLettres = résultat Exit Function centaine_dizaine: '*** Subroutine for conversion into letters ' '*** of hundreds and tens ' varlet = "" ' '*** Processing hundreds ' If varnum >= 100 Then varlet = chiffre(Int(varnum / 100)) varnum = varnum Mod 100 If varlet = "one" Then varlet = "hundred " Else varlet = varlet + " hundred " End If End If ' '*** Processing tens ' If varnum <= 19 Then '*** Case where the ten is <20 If varnum > 0 Then varlet = varlet + chiffre(varnum) Else '*** Other cases varnumD = Int(varnum / 10) '*** ten's digit varnumU = varnum Mod 10 '*** unit's digit '''Select Case varnumD '*** generating tens in letters varlet = varlet + dizaine(varnumD) '''Case Is <= 5 '''varlet = varlet + dizaine(varnumD) '''Case 6, 7 '''varlet = varlet + dizaine(6) '''Case 8, 9 '''varlet = varlet + dizaine(8) '''End Select ' '*** Processing the separator of tens and units ' If varnumU = 1 And varnumD < 8 Then varlet = varlet + " and " Else If varnumU <> 0 Then varlet = varlet + "-" '''Or varnumD = 7 Or varnumD = 9 End If End If ' '*** Generating units ' '''If varnumD = 7 Or varnumD = 9 Then varnumU = varnumU + 10 If varnumU <> 0 Then varlet = varlet + chiffre(varnumU) End If ' '*** Removing leading spaces and returning ' varlet = RTrim(varlet) Return End Function
Not bad the code but I found 2 bugs
100 = cents instead of cent and same for 20
good luck
See you+