CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    [RESOLVED] Adding Numbers Together (you won't believe this)

    Normally, if you have numbers in text boxes you can say

    Code:
    TextBox3.text = STR( val(textbox1.text) + val(textbox2.text))
    eg, 5 = 2 + 3

    This has been the old faithful simple addition for as long as I can remember

    I have suddenly found this does not work, if the text box contains a formatted value - namely

    When Textbox1 contains 1,000
    Textbox2 contains 2,000

    The answer becomes 3 (NOT 3000)

    Is there a simple way to deal with this problem, without running REPLACE, for example





    PS Why do I have to scroll the screen across to find the SUBMIT NEW THREAD Button
    (Is it my browser IE8)

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Adding Numbers Together (you won't believe this)

    Convert to decimal, or don't allow anything but numbers
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Adding Numbers Together (you won't believe this)

    Got it

    I should use CCur instead of Val


    TextBox3.text = STR( CCur(textbox1.text) + CCur(textbox2.text))



    (where have the formatting controls for the forum gone)

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [RESOLVED] Adding Numbers Together (you won't believe this)

    I would also advise using CStr() rather than the old STR() function

    CStr() will not add the leading space that STR() places in there.

    Val() is dangerous in many cases, it reads the value left to right and stops when it finds a non numeric character


    24ZY3 would return 24 if val was used on it even though the string is not a valid number at all.
    Always use [code][/code] tags when posting code.

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

Featured