My next challenge is to convert Numbers to VBscript. Why? To learn VBscript.
It is complex but not too complex to begin with. If you have ideas, please share them
Thanx
Based on what? How do the numbers relate to the words? A combination of character ascii codes, or... what? I guess what I'm trying to get at, is... what would this be useful for, and how would one use it?
That is a whole lot of code for what it does, though. And what exactly is the purpose of the opacity thing, or was that just to try it out or something?
If code has GOTOs in it not involving error handling, I GOTO something else.
It saves lots of hair-pulling and frustration.
But thanks anyway.
Goto's are generally bad, in my opinion. I rarely(and I mean RARELY) use them. If you can avoid them, do so. And in almost every case, they ARE avoidable. It's just bad programming practice, in my honest opinion.
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
This thread has really been bugging me. I guess maybe it was the challenge. Anyway, I had to get this code out of my head and into my machine.
I feel better now.
I didn’t spend much time streamlining the code. Nor was I shooting for elegance. I’m sure it can be fine tuned. I just wanted to get it functional.
This code does what is advertised, but only up to 28 digits. After that the values automagically convert to scientific notation. I'm sure there is a way past this, and I've set the program up to process values up to 10^100 (a centillion or a googol, depending on who you ask).
Numbers are infinite, but computer mathematics and my patience are not. There is, thankfully, a limit as to how far one can go with this. Myself, I have no interest in going further than named numbers.
But I am interested to know what’s wrong, and fixing it.
VB Code:
' Returns in english words any decimal value entered up to 28 digits.
' (Assumes U.S. numbering system)
' You'll need three text boxes:
' txtEntry
' txtFormattedEntry
' txtResult ' make this one big, with multiLine = True
' And one buton
' cmdProcess
Option Explicit
Private SuffixArray(35) As String
Private Sign As String
Private Sub cmdProcess_Click()
Dim DecimalLoc As Integer
Dim DecimalPart As String
Dim WholeNumberPart As String
Dim VerifiedNumber As String
Dim x As Integer
Dim y As Integer
Dim Result As String
Dim LeftDigit As String
Dim MiddileDigit As String
Dim RightDigit As String
' clear old stuff
txtResult.Text = ""
txtFormattedEntry.Text = ""
' make sure it's a number
VerifiedNumber = ValidateNumber
' if it's not a valid number then get out
If Len(VerifiedNumber) = 0 Then
MsgBox "Text entry is not numeric.", vbExclamation, ""
Exit Sub
End If
' limit of unknown origin
If Len(VerifiedNumber) > 28 Then
MsgBox "Number entered its too large.", vbExclamation, ""
The code should not really be all that long, to be honest. The names are the longest part of it, but you could get around that by saving them to a txt file and parsing them that way, rather than creating lines and lines of code. Generally, when I'm working with a structured setting like that(i.e. 10 = "Ten", 11 = "Eleven", or 10^3 = "Thousand", 10^6 = "Million", etc.), I'd create a database file to reference, rather than assigning values or variables for each one and referencing them that way. That's too much work, too many lines of code to keep track of. I'd rather just setup a file, and write a couple of lines to parse it accordingly. As far as searching the string, it would all be a matter of finding the reference point of how many numbers it contains(i.e. thousands, millions, billions, etc.), and converting the numbers to text according to the reference for each in the database.
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
Thanks for your input, guys. In my post, I did state that this was just code to get it to work. Not shooting for any prizes for least lines of code, efficiency or elegance here. Reading from text files (or creating a database to hold names) is beyond the scope and requirements of such a small, trivial application (It's less than 200 lines of code!).
It's just code to get the job done.
This type of thing happens a lot in the real world. It's not recommended, but such is life.
Anyway, I was hoping for some help regarding the unexpected limitations.
Ok, I can tell by the vast amount of traffic and replies that this thread has been getting since I posted the new & improved version, and asked for assistance (tap, tap, tap, hello, is this thing on?) that everyone has been waiting with unbearable anticipation to know exactly what was wrong with my code.
So without further ado: I was converting the input string to a double to use Format(). I tore it out, and now it all works. Enter up to 102 digits into the application and zing-bam-zing you get the English equivalent.
What could you use it for, you ask?
Why writing checks of course!
So, just in case you want to pay off every debt that's ever been made, and any future ones for all mankind, you WILL need this app. Trust me on this!
RJ, I know you spent much time on this, and you still wrote in "New School"
Just append "teen" to the number ...etc
I am impressed you found numbers beyond 66 digits.
And if you want some to write some code, look at my "Text to Excel"...I need to add some user controls like ListView...interested?
My version, handles the digits in groups of three or power of one thousand.
NumToWord(ByVal NumStr As String, [KiloDelimiter As String = ","], [Conversion As VbStrConv = vbProperCase]) As String
NumStr
- additional validation and string processing
- converted to integer array in groups of 3 starting with least significant digits
KiloDelimeter
- delimeter used on words grouped by power of thousand, default is comma
Conversion
- value passed on to StrConv()
Remarks:
- Sample implementation handled only converion to words of whole number part, decimal portion handling is left as case to case. Power_ToWord() can be used for negative powers of one thousand (eg. millionth) if you choose to handle the decimal portion that way.
One can always make a better mouse trap.
Like I said, a client wrote this in the eighties (84?) on an Alpha Micro (AlphaBasic).
While fixing several Y2K clients, I found this code, and converted to Qbasic...fifteen minutes.
Then last year my niece was asking about numbers, and how big do they become.
Being the good uncle, I converted it again...thirty minutes. http://www.vbforums.com/showthread.php?t=443956
I had some problems with the Data statements.
Enjoy
I'm not belittling anyone else's approach, in fact I used it as basis in the interest of providing other members with an updated approach.
As to the decimals, I still believe its case to case... it could be units based such as cents, microns, nanoseconds, etc. The module can be updated to handle such special cases, just create an appropriate procedure and call it. Such as accepting strDecimal as an argument then converting it to microns (up to implementor if the new decimals from shift to microns will be dropped).
EDIT: I updated the source for converting numbers to their word (or english or spelled) equivalents. Made it leaner and it now uses just three procedures. There is also the option to suppress prompting the user if errors were encountered.
Attached is the update. Feel free to debug and/or comment.
EDIT 2008-08-29: Previous update accidentally shifted to Trim instead of LTrim in removing leading zeroes. Code updated. Thanks Keithuk for heads up
Last edited by leinad31; Aug 29th, 2008 at 08:10 AM.
hi i am lokking for a word converter as same like this but i need the decimal also.ex12.5 twelve point five
or26.25 =twentysix point two five so can u help me?
I'm not belittling anyone else's approach, in fact I used it as basis in the interest of providing other members with an updated approach.
As to the decimals, I still believe its case to case... it could be units based such as cents, microns, nanoseconds, etc. The module can be updated to handle such special cases, just create an appropriate procedure and call it. Such as accepting strDecimal as an argument then converting it to microns (up to implementor if the new decimals from shift to microns will be dropped).
EDIT: I updated the source for converting numbers to their word (or english or spelled) equivalents. Made it leaner and it now uses just three procedures. There is also the option to suppress prompting the user if errors were encountered.
Attached is the update. Feel free to debug and/or comment.
EDIT 2008-08-29: Previous update accidentally shifted to Trim instead of LTrim in removing leading zeroes. Code updated. Thanks Keithuk for heads up
the number- 2,59,082
it shows:- Two Hundred Fifty Nine Thousand, Eighty Two
How do i convert like this: Two Lakh Fifty Nine thousand, Eighty two only
Last edited by Eliminator2009; Jul 27th, 2010 at 06:11 AM.
I belive in pray. I pray for you.
You may have happy, healthy, wealthy life.
hope this will help you.. for Convert Number to Word (Indian Format)... Full source code Num2Word.zip Here.. You can modify it with what you need.. The Code also cover the Indian Number format issue.. ie. 12,34,563 instead of 123,456,123.
I didn't write this function, but I did fix some bugs and greatly expand the highest number possible (as well as change data types to handle it, among other things). It currently handles numbers up to 606 digits long (1 duocenuntillion - 1), after that it returns a number in terms of the largest unit in its array. Also I did this over a decade ago when I was a teenager. So go easy on me, I know it needs updating, but I rarely use it and it works as-is.
Code:
Public Function ConvertNumberToText(ByVal aNumber As String) As String
On Error Resume Next
'handles numbers natively from zero all the way through (1 duocenuntillion - 1)
'larger numbers are accepted, but are named in the highest available unit
If (aNumber$ = "0") Or (aNumber = "-0") Then
ConvertNumberToText = "Zero"
Exit Function
End If
Dim bNeg As Boolean
Dim aNames As Variant
Dim iNameCounter%, aNumberName$, aTempRet$, bNeedHyphen As Boolean, iTemp%, szTemp As String, szTR As String
aNumber = Replace(aNumber, ",", "") 'no commas in the input, k thx
aNumber = Replace(aNumber, " ", "") 'or spaces
If Left(aNumber, 1) = "-" Then
aNumber = Mid$(aNumber, 2)
bNeg = True
End If
If Len(aNumber) > 606 Then
szTemp = Left(aNumber, Len(aNumber) - 603)
aNumber = Right(aNumber, 603)
szTR = ConvertNumberToText(szTemp)
End If
If Len(aNumber) > 3 Then
aNames = Array("", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", _
"Decillion", "Undecillion", "Duodecillion", "Tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septendecillion", "Octodecillion", "Novemdecillion", _
"Vigintillion", "Unvigintillion", "Duovigintillion", "Trevigintillion", "Quattuorvigintillion", "Quinvigintillion", "Sexvigintillion", "Septenvigintillion", "Octovigintillion", "Novemvigintillion", _
"Trigintillion", "Untrigintillion", "Duotrigintillion", "Tretrigintillion", "Quattuortrigintillion", "Quintrigintillion", "Sextrigintillion", "Septtrigintillion", "Octotrigintillion", "Novemtrigintillion", _
"Quadragintillion", "Unquadragintillion", "Duoquadragintillion", "Trequadragintillion", "Quattuorquadragintillion", "Quinquadragintillion", "Sexquadragintillion", "Septquadragintillion", "Octoquadragintillion", "Novemquadragintillion", _
"Quinquagintillion", "Unquinquagintillion", "Duoquinquagintillion", "Trequinquagintillion", "Quattuorquinquagintillion", "Quinquinquagintillion", "Sexquinquagintillion", "Septquinquagintillion", "Octoquinquagintillion", "Novemquinquagintillion", _
"Sexagintillion", "Unsexagintillion", "Duosexagintillion", "Tresexagintillion", "Quattuorsexagintillion", "Quinsexagintillion", "Sexsexagintillion", "Septsexagintillion", "Octosexagintillion", "Novemsexagintillion", _
"Septuagintillion", "Unseptuagintillion", "Duoseptuagintillion", "Treseptuagintillion", "Quattuorseptuagintillion", "Quinseptuagintillion", "Sexseptuagintillion", "Septseptuagintillion", "Octoseptuagintillion", "Novemseptuagintillion", _
"Octogintillion", "Unoctogintillion", "Duooctogintillion", "Treoctogintillion", "Quattuoroctogintillion", "Quinoctogintillion", "Sexoctogintillion", "Septoctogintillion", "Octooctogintillion", "Novemoctogintillion", _
"Nonagintillion", "Unnonagintillion", "Duononagintillion", "Trenonagintillion", "Quattuornonagintillion", "Quinnonagintillion", "Sexnonagintillion", "Septnonagintillion", "Octononagintillion", "Novemnonagintillion", _
"Centillion", "Cenuntillion", "Cenduotillion", "Centretillion", "Cenquattuortillion", "Cenquintillion", "Censextillion", "Censeptentillion", "Cenoctotillion", "Cennovemtillion", _
"Cendecillion", "Cenundecillion", "Cenduodecillion", "Centredecillion", "Cenquattuordecillion", "Cenquindecillion", "Censexdecillion", "Censeptendecillion", "Cenoctodecillion", "Cennovemdecillion", _
"Cenvigintillion", "Cenunvigintillion", "Cenduovigintillion", "Centrevigintillion", "Cenquattuorvigintillion", "Cenquinvigintillion", "Censexvigintillion", "Censeptenvigintillion", "Cenoctovigintillion", "Cennovemvigintillion", _
"Centrigintillion", "Cenuntrigintillion", "Cenduotrigintillion", "Centretrigintillion", "Cenquattuortrigintillion", "Cenquintrigintillion", "Censextrigintillion", "Censeptentrigintillion", "Cenoctotrigintillion", "Cennovemtrigintillion", _
"Cenquadragintillion", "Cenunquadragintillion", "Cenduoquadragintillion", "Centrequadragintillion", "Cenquattuorquadragintillion", "Cenquinquadragintillion", "Censexquadragintillion", "Censeptenquadragintillion", "Cenoctoquadragintillion", "Cennovemquadragintillion", _
"Cenquinquagintillion", "Cenunquinquagintillion", "Cenduoquinquagintillion", "Centrequinquagintillion", "Cenquattuorquinquagintillion", "Cenquinquinquagintillion", "Censexquinquagintillion", "Censeptenquinquagintillion", "Cenoctoquinquagintillion", "Cennovemquinquagintillion", _
"Censexagintillion", "Cenunsexagintillion", "Cenduosexagintillion", "Centresexagintillion", "Cenquattuorsexagintillion", "Cenquinsexagintillion", "Censexsexagintillion", "Censeptensexagintillion", "Cenoctosexagintillion", "Cennovemsexagintillion", _
"Censeptuagintillion", "Cenunseptuagintillion", "Cenduoseptuagintillion", "Centreseptuagintillion", "Cenquattuorseptuagintillion", "Cenquinseptuagintillion", "Censexseptuagintillion", "Censeptenseptuagintillion", "Cenoctoseptuagintillion", "Cennovemseptuagintillion", _
"Cenoctogintillion", "Cenunoctogintillion", "Cenduooctogintillion", "Centreoctogintillion", "Cenquattuoroctogintillion", "Cenquinoctogintillion", "Censexoctogintillion", "Censeptenoctogintillion", "Cenoctooctogintillion", "Cennovemoctogintillion", _
"Cennonagintillion", "Cenunnonagintillion", "Cenduononagintillion", "Centrenonagintillion", "Cenquattuornonagintillion", "Cenquinnonagintillion", "Censexnonagintillion", "Censeptennonagintillion", "Cenoctononagintillion", "Cennovemnonagintillion", _
"Duocentillion")
'The question is how high we should really go...
aNumber = String$(3 - Len(aNumber) Mod 3, "0") & aNumber
Do
aTempRet = ConvertNumberToText(Right$(aNumber, 3))
If Len(aTempRet) > 0 Then
If iNameCounter > 0 Then
aNumberName = aTempRet & " " & aNames(iNameCounter) & ", " & aNumberName
Else
aNumberName = aTempRet
End If
End If
aNumber = Left$(aNumber, Len(aNumber) - 3)
iNameCounter = iNameCounter + 1
Loop Until Len(aNumber) = 0
Else
aNumber = Trim$(Val(aNumber))
If Len(aNumber) = 3 Then
aNumberName = Choose(Val(Left$(aNumber, 1)), "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine") & " Hundred "
aNumber = Trim$(Val(Right$(aNumber, 2)))
End If
If Len(aNumber) = 2 Then
iTemp = Val(Left$(aNumber, 1))
If iTemp > 1 Then
aNumberName = aNumberName & Choose(iTemp, "", "Twenty", "Thrirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty")
aNumber = Trim$(Val(Right$(aNumber, 1)))
bNeedHyphen = True
Else
aNumberName = aNumberName & Choose(Val(Right$(aNumber, 1)) + 1, "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
aNumber = ""
End If
End If
If Len(aNumber) = 1 Then
iTemp = Val(Left$(aNumber, 1))
If iTemp > 0 Then
If bNeedHyphen Then aNumberName = aNumberName & "-"
aNumberName = aNumberName & Choose(iTemp, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine")
End If
End If
End If
If szTR <> "" Then
'szTR = Left(szTR, Len(szTR) - 1) 'trim last comma
aNumberName = szTR & " Duocentillion, " & aNumberName
End If
szTR = RTrim$(aNumberName)
If Right(szTR, 1) = "," Then
szTR = Left(szTR, Len(szTR) - 1)
End If
If bNeg Then szTR = "Negative " & szTR
ConvertNumberToText = szTR
End Function
ALSO...
There is an algorithm to name ANY number (>10^3000003; millia is the highest prefix, so it gets to milliamillia...), but it's fairly complex, and the only example I've ever found was written in CGI. It would take far longer than I'm able to spend for me to figure out how to do it in VB, but if someone else was interested, here's what I'm referring to: http://www.isthe.com/chongo/tech/mat...r/howhigh.html for the explanations; and the source of the script: http://www.isthe.com/chongo/tech/math/number/number (working version is on the site).
If anyone ever converts that algorithm to VB, please PM me!
Try this Simple Indian Style Programming (Luv Manish Bhardwaj)
=================================================
Private Sub BillSave_Click()
Dim num As Long 'Number
Dim length, r, ctr, o, t, h, th, tth, l, tl, c, tc As Integer 'Variables to store o,t,h,th,tth,l,ten lakh etc.
'================================First Part====================================================
num = Val(AmountR.Text) 'Take your number
length = Len(AmountR.Text) 'Find the length
While num >= 1 'Loop until num is greter or = 1
ctr = ctr + 1
r = num Mod 10 'Find the remainder after dividing by 10 so as to get last digit
If ctr = 1 Then
o = r 'First value is Once
ElseIf ctr = 2 Then
t = r 'Second is Tens
ElseIf ctr = 3 Then
h = r 'Third is Hundred
ElseIf ctr = 4 Then
th = r
ElseIf ctr = 5 Then
tth = r
ElseIf ctr = 6 Then
l = r
ElseIf ctr = 7 Then
tl = r
ElseIf ctr = 8 Then
c = r
ElseIf ctr = 9 Then
tc = r 'Until Ten Crore
End If
num = Fix(num / 10) 'new number is except last digit eg. if num is 1234 here num will be 123
Wend
'================================Second Part====================================================
Dim str As String
Dim res As String
str = ""
res = ""
While length >= 1 'Loop until all the digits
If length = 9 And tc > 1 Then
res = findfig(tc * 10) 'Calling function to find TenCrore's WordValue Multiply by 10 to get ten, twenty
str = str + res 'Adding result into a string
ElseIf length = 8 Then
If tc = 1 Then
res = findfig(c + 10) 'Adding Ten to crore value to get 11,12,13 etc
Else
res = findfig(c)
End If
If tc = 0 And c = 0 Then 'Display when tc and c both are not zero
Else
str = str + res + " Crore "
End If
ElseIf length = 7 And tl > 1 Then
res = findfig(tl * 10)
str = str + res
ElseIf length = 6 Then
If tl = 1 Then
res = findfig(l + 10)
Else
res = findfig(l)
End If
If tl = 0 And l = 0 Then
Else
str = str + res + " Lakh "
End If
ElseIf length = 5 And tth > 1 Then
res = findfig(tth * 10)
str = str + res
ElseIf length = 4 Then
If tth = 1 Then
res = findfig(th + 10)
Else
res = findfig(th)
End If
If tth = 0 And th = 0 Then
Else
str = str + res + " Thousands "
End If
ElseIf length = 3 Then
res = findfig(h)
If Not h = 0 Then
str = str + res + " Hundred "
End If
ElseIf length = 2 And t > 1 Then
res = findfig(t * 10)
str = str + res
ElseIf length = 1 Then
If t = 1 Then
res = findfig(o + 10)
Else
res = findfig(o)
End If
str = str + res + " Only"
End If
length = length - 1
Wend
AmtWords.Caption = str 'Use str as final Result
End Sub
Public Function findfig(ByVal length As Integer) As String
Dim str As String
str = ""
Select Case length
Case 1
str = "One"
Case 2
str = "Two"
Case 3
str = "Three"
Case 4
str = "Four"
Case 5
str = "Five"
Case 6
str = "Six"
Case 7
str = "Seven"
Case 8
str = "Eight"
Case 9
str = "Nine"
Case 10
str = "Ten"
Case 11
str = "Eleven"
Case 12
str = "Twelve"
Case 13
str = "Thirteen"
Case 14
str = "Fourteen"
Case 15
str = "Fifteen"
Case 16
str = "Sixteen"
Case 17
str = "Seventeen"
Case 18
str = "Eightteen"
Case 19
str = "Nineteen"
Case 20
str = "Twenty "
Case 30
str = "Thirty "
Case 40
str = "Forty "
Case 50
str = "Fifty "
Case 60
str = "Sixty "
Case 70
str = "Seventy "
Case 80
str = "Eighty "
Case 90
str = "Ninety "
End Select
findfig = str
End Function
manishxy , there is always room for improvement, like I posted, this was written in, Alpha Basic on an Alpha Micro system in 1984?
Then converted to Qbasic, thank for looking it up....smiles