Results 1 to 5 of 5

Thread: FormatCurrency not coming out correctly.

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Resolved 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:
    1. 'Variables are defined  
    2.         Dim intNumberItems As Integer = 0 'Var from inputbox CInt
    3.         Dim intCount As Integer           'The loop counter
    4.         Dim intTotal As Integer           'The accumulator
    5.         intCount = 1                      'Counter set to value
    6.         intTotal = 0                      'The accumulator set to value
    7.         Dim strNumberItems As String      'Variable used for InputBox(Items)
    8.         Dim strPrice As String            'Variable used for InputBox (Total)
    9.  
    10.         'Open initial input box to inquire how many items need to be input
    11.         strNumberItems = InputBox("How many items do you wish to input? ", _
    12.         "Enter Items")
    13.         intNumberItems = CInt(strNumberItems)
    14.  
    15.  
    16.         'Set up loop to continue to show strPrice input box until
    17.         'intCount is equal to intNumberItems
    18.         Do Until intCount > intNumberItems
    19.  
    20.             strPrice = InputBox("Enter the value of item # " & intCount.ToString _
    21.             & " into the box", "Enter item Value")
    22.  
    23.  
    24.             'Set intCount to count
    25.             intCount += 1
    26.             intTotal += CInt(strPrice)
    27.  
    28.         Loop
    29.  
    30.  
    31.         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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    VB Code:
    1. myNumber.ToString("c")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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
    VB Code:
    1. formatCurrency(blah)
    or
    VB Code:
    1. blah.ToString("c")
    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
    VB Code:
    1. blah.ToString("c")
    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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
  •  



Click Here to Expand Forum to Full Width