Jeg har forsøgt at omskrive nedenstående til danske talord, men bøvler med, at det sidste tal i 10'erne.
Tallet 3.279.600 bliver til: Tre Million ToHundredNiogHalvfjerds Ni Tusind SeksHundredFireogTredive Fire Kroner
Kan nogen se fejlen?
'Main Function Function SpellNumber(ByVal MyNumber) Dim Dollars, Cents, Temp Dim DecimalPlace, Count ReDim Place(9) As String Place(2) = " Tusind " Place(3) = " Million " Place(4) = " Billion " Place(5) = " Trillion " ' 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 = GetTens(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 = "No Dollar" Case "One" Dollars = "One Dollar" Case Else Dollars = Dollars & " Kroner" End Select Select Case Cents Case "" Cents = "" Case "One" Cents = " and One Cent" Case Else Cents = " and " & Cents & " Cents" 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 Mid(MyNumber, 1, 1) <> "0" Then Result = GetDigit(Mid(MyNumber, 1, 1)) & "Hundred" End If ' Convert the tens and ones place. If Mid(MyNumber, 2, 1) <> "0" Then Result = Result & GetDigit(Mid(MyNumber, 3)) & "og" Result = Result & GetTens(Mid(MyNumber, 2)) Else Result = Result & GetTens(Mid(MyNumber, 2)) 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 = "Ti" Case 11: Result = "Elleve" Case 12: Result = "Tolv" Case 13: Result = "Tretten" Case 14: Result = "Fjorten" Case 15: Result = "Femten" Case 16: Result = "Seksten" Case 17: Result = "Sytten" Case 18: Result = "Etten" Case 19: Result = "Nitten" Case Else End Select Else ' If value between 20-99... Select Case Val(Left(TensText, 1)) Case 2: Result = "Tyve " Case 3: Result = "Tredive " Case 4: Result = "Fyrre " Case 5: Result = "Halvtreds " Case 6: Result = "Tres " Case 7: Result = "Halvfjerds " Case 8: Result = "Firs " Case 9: Result = "Halvfems " Case Else End Select Result = Result & 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 = "Et" Case 2: GetDigit = "To" Case 3: GetDigit = "Tre" Case 4: GetDigit = "Fire" Case 5: GetDigit = "Fem" Case 6: GetDigit = "Seks" Case 7: GetDigit = "Syv" Case 8: GetDigit = "Otte" Case 9: GetDigit = "Ni" Case Else: GetDigit = "" End Select End Function '
#1 - Der var alligevel en lille fejl i koden i #1. Herunder er en tilrettet Function.
Option Explicit
Function SpellNumber(ByVal MyNumber) Dim Dollars, Cents, Temp Dim DecimalPlace, Count ReDim Place(9) As String Place(2) = " Tusinde " Place(3) = " Millioner " Place(4) = " Billion " Place(5) = " Trillion " ' 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 = GetTens(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 = "No Dollar" Case "One" Dollars = "One Dollar" Case Else Dollars = Dollars & " Kroner" End Select Select Case Cents Case "" Cents = "" Case "One" Cents = " and One Cent" Case Else Cents = " and " & Cents & " Cents" 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 Mid(MyNumber, 1, 1) <> "0" Then Result = GetDigit(Mid(MyNumber, 1, 1)) & "Hundrede" & "og" End If ' Convert the tens and ones place. If Mid(MyNumber, 2, 1) <> "0" Then Result = Result & GetDigit(Mid(MyNumber, 3)) & "og" Result = Result & GetTens(Mid(MyNumber, 2)) Else Result = Result & GetTens(Mid(MyNumber, 2)) 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 = "Ti" Case 11: Result = "Elleve" Case 12: Result = "Tolv" Case 13: Result = "Tretten" Case 14: Result = "Fjorten" Case 15: Result = "Femten" Case 16: Result = "Seksten" Case 17: Result = "Sytten" Case 18: Result = "Etten" Case 19: Result = "Nitten" Case Else End Select Else ' If value between 20-99... Select Case Val(Left(TensText, 1)) Case 2: Result = "Tyve" Case 3: Result = "Tredive" Case 4: Result = "Fyrre" Case 5: Result = "Halvtreds" Case 6: Result = "Tres" Case 7: Result = "Halvfjerds" Case 8: Result = "Firs" Case 9: Result = "Halvfems" Case Else End Select Result = Result & GetDigit _ (Right(TensText, 2)) ' 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 = "Et" Case 2: GetDigit = "To" Case 3: GetDigit = "Tre" Case 4: GetDigit = "Fire" Case 5: GetDigit = "Fem" Case 6: GetDigit = "Seks" Case 7: GetDigit = "Syv" Case 8: GetDigit = "Otte" Case 9: GetDigit = "Ni" Case Else: GetDigit = "" End Select End Function '
Hej. Jeg er lige fakdet over dette opslag, og har prøvet at bruge den, men den skriven Fks. EtOgTyve i stedet for EnOgTyve, er der nogen af jer der har en løsning på dette ? så der stadig kan skrives EtHundred
Det er rigtig fint, men så skriver den jo EnHundred og det hedder jo Ethundred, så det er ikke helt den løsning jeg er på jagt efter. Jeg skulle gerne have mulighed for både at kunne få tallet Ethundred og tallet EthundreOgEnOgTyve. Håber det giver mening ?
#5 - Det er jo et af "problemerne" ved det danske sprog, sammenlignet med Engelsk. Vi har jo både "En" og "Et" hvor der kun er "One" i det engelske, hvor det nærmeste ville være at sammenligne med "Single" (A single Dollar).
Så jeg tror ikke du vil kunne få en løsning, som dækker problematikken. Hvordan skulle koden kunne forstå, at det nu er "En" eller "Et".
Jeg er helt enig. Det var jo derfor jeg stillede spørgsmålet, i håb om at der var en der havde fundet en løsning.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.