Ranjit Goray
January 3rd, 2000, 09:55 PM
How do i convert a number field..i.e in case the no is 5,432 then i want to print it as Five Thousand Four Hundred and Thirty Two.
|
Click to See Complete Forum and Search --> : Converting Numbers to Words Ranjit Goray January 3rd, 2000, 09:55 PM How do i convert a number field..i.e in case the no is 5,432 then i want to print it as Five Thousand Four Hundred and Thirty Two. Nerea January 5th, 2000, 02:48 AM Searching different web pages, I found the next address: http://www.vb-world.net/tips/tip108.html Hope it will help you. czimmerman January 5th, 2000, 04:52 PM See http://www.freevbcode.com/ShowCode.Asp?ID=343 Chris Eastwood January 6th, 2000, 02:52 AM Here's a sample I've had on my harddisk for longer than I can remember (hence, I can't remember who originally wrote it). It takes a 'double' value (eg. 12345.67) and returns the value such as "One thousand, two hundred thirty-four and 56/100" : (note that the numbering is in US format for millions and trillions - you may need to change this depending on your location). private Function NumToText(dblVal as Double) as string static sOnes(0 to 9) as string static sTeens(0 to 9) as string static sTens(0 to 9) as string static sThousands(0 to 4) as string static bInit as Boolean ' Dim i as Integer Dim bAllZeros as Boolean Dim bShowsThousands as Boolean Dim sValue as string Dim sBuffer as string Dim sTemp as string Dim iCol as Integer Dim iChar as Integer ' ' Warning - this routine only handles positive values ' Debug.Assert dblVal > 0 ' If bInit = false then 'Initialize array bInit = true sOnes(0) = "zero" sOnes(1) = "one" sOnes(2) = "two" sOnes(3) = "three" sOnes(4) = "four" sOnes(5) = "five" sOnes(6) = "six" sOnes(7) = "seven" sOnes(8) = "eight" sOnes(9) = "nine" sTeens(0) = "ten" sTeens(1) = "eleven" sTeens(2) = "twelve" sTeens(3) = "thirteen" sTeens(4) = "fourteen" sTeens(5) = "fifteen" sTeens(6) = "sixteen" sTeens(7) = "seventeen" sTeens(8) = "eighteen" sTeens(9) = "nineteen" sTens(0) = "" sTens(1) = "ten" sTens(2) = "twenty" sTens(3) = "thirty" sTens(4) = "forty" sTens(5) = "fifty" sTens(6) = "sixty" sTens(7) = "seventy" sTens(8) = "eighty" sTens(9) = "ninety" sThousands(0) = "" sThousands(1) = "thousand" 'US numbering sThousands(2) = "million" sThousands(3) = "billion" sThousands(4) = "trillion" End If ' ' Setup error Handler ' on error GoTo vbErrorHandler ' ' get fractional part of value (if any) ' sBuffer = "and " & Format$((dblVal - Int(dblVal)) * 100, "00") & "/100" ' ' Convert main part to string ' sValue = CStr(Int(dblVal)) ' bAllZeros = true ' for i = len(sValue) to 1 step -1 iChar = Val(mid$(sValue, i, 1)) iCol = (len(sValue) - i) + 1 ' 'Action depends on 1's, 10's or 100's column ' Select Case (iCol Mod 3) Case 1 '1's position bShowsThousands = true If i = 1 then sTemp = sOnes(iChar) & " " ElseIf mid$(sValue, i - 1, 1) = "1" then sTemp = sTeens(iChar) & " " i = i - 1 ElseIf iChar > 0 then sTemp = sOnes(iChar) & " " else bShowsThousands = false If mid$(sValue, i - 1, 1) <> "0" then bShowsThousands = true ElseIf i > 2 then If mid$(sValue, i - 2, 1) <> "0" then bShowsThousands = true End If End If sTemp = "" End If If bShowsThousands then If iCol > 1 then sTemp = sTemp & sThousands(iCol \ 3) If bAllZeros then sTemp = sTemp & " " else sTemp = sTemp & ", " End If End If bAllZeros = false End If sBuffer = sTemp & sBuffer Case 2 If iChar > 0 then If mid$(sValue, i + 1, 1) <> "0" then sBuffer = sTens(iChar) & "-" & sBuffer else sBuffer = sTens(iChar) & " " & sBuffer End If End If Case 0 If iChar > 0 then sBuffer = sOnes(iChar) & " hundred " & sBuffer End If End Select next i ' sBuffer = UCase$(Left$(sBuffer, 1)) & mid$(sBuffer, 2) ' EndNumToText: NumToText = sBuffer Exit Function ' vbErrorHandler: sBuffer = "#error#" resume EndNumToText End Function Chris Eastwood CodeGuru - the website for developers http://codeguru.developer.com/vb codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |