|
-
Feb 20th, 2007, 03:02 AM
#1
Thread Starter
Addicted Member
FormatCurrency not coming out correctly.
Hello All,
I have a quick question for everyone here. I have written a small program here using input boxes and a loop. The program goes off without a hitch but I can't seem to get the correct total to come up when working with cents. If I use whole dollar values then all is well but if a cent value is added to the program then the math isn't done correctly. Can any one of you fine gents give me a hand by skimming over the code I have so far?
VB Code:
'Variables are defined
Dim intNumberItems As Integer = 0 'Var from inputbox CInt
Dim intCount As Integer 'The loop counter
Dim intTotal As Integer 'The accumulator
intCount = 1 'Counter set to value
intTotal = 0 'The accumulator set to value
Dim strNumberItems As String 'Variable used for InputBox(Items)
Dim strPrice As String 'Variable used for InputBox (Total)
'Open initial input box to inquire how many items need to be input
strNumberItems = InputBox("How many items do you wish to input? ", _
"Enter Items")
intNumberItems = CInt(strNumberItems)
'Set up loop to continue to show strPrice input box until
'intCount is equal to intNumberItems
Do Until intCount > intNumberItems
strPrice = InputBox("Enter the value of item # " & intCount.ToString _
& " into the box", "Enter item Value")
'Set intCount to count
intCount += 1
intTotal += CInt(strPrice)
Loop
lblTotalAmount.Text = FormatCurrency(intTotal)
Thank you so much, you have no idea how much I value this site,
Abe
VB 2005 Express Edition
Last edited by Abrium; Feb 20th, 2007 at 04:04 AM.
Reason: Forgot to add version being used
-
Feb 20th, 2007, 03:11 AM
#2
Re: FormatCurrency not coming out correctly.
What is an Integer? It's a whole number. Of course you can't put decimal places in an Integer. That's mathematics, not programming. You should normally use the Decimal type for currency.
Also, don't use FormatCurrency at all. To convert a number to currency use:
-
Feb 20th, 2007, 03:35 AM
#3
Thread Starter
Addicted Member
Re: FormatCurrency not coming out correctly.
To be honest I can't believe I missed that... Good point. If your going to use currency format for output it would some what HELP to have a conversion that allowed for decimal places eh?
As far as the difference between using or I have never even seen that method being used. I have been head strong into this for all of a month now so the only conversion systems I have read about or seen used in examples in the texts I have bought are those like the type I used.
What is the advantage of using vs the way I was converting it?
Thanks for the insite on decimals though J its going on 3 in the morning here and I just plain didn't see that...
Abe
-
Feb 20th, 2007, 03:55 AM
#4
Re: FormatCurrency not coming out correctly.
Many VB.NET developers, including those who write books, have previously developed in VB6. Many of those continue to use the functions that they're used to from VB6, mnay of which have been implemented in the .NET Framework for "convenience". FormatCurrency is one of those. Now, there is nothing inherently wrong with using FormatCurrency or its ilk but the question that should be asked is "what advantage does FormatCurrency offer over the standard, System-based alternative". The answer is none. ToString("c") will work in any .NET language and does exactly the job that you want. FormatCurrency was included in the VB.NET Runtime to make VB6 developers feel less intimidated by the change. You've never developed in VB6 so there's no reason or need for you to use VB6-style methods.
-
Feb 20th, 2007, 04:02 AM
#5
Thread Starter
Addicted Member
Re: FormatCurrency not coming out correctly.
Good information
Thank you for the update and the insite on the moron step of trying to use currency with integers..
Until next post (which shouldn't be long)
Abe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|