CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    CInt rounds the number, but i need to display without rounding

    When I use CInt(Text1) + CInt(Text2) in a formula, it rounds the answer..i need my answer without rounding......
    Thank You


  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: CInt rounds the number, but i need to display without rounding

    How about


    Dim dblX as Double
    Dim dblY as Double

    If IsNumeric(Text1.Text) then
    dblX = CDbl(Text1.Text)
    else
    MsgBox "Text1.Text Not Numeric"
    Exit sub/function/whatever
    End if

    If IsNumeric(Text2.Text) then
    dblY = CDbl(Text2.Text)
    else
    MsgBox "Text2.Text Not Numeric"
    Exit sub/function/whatever
    End if

    MsgBox "Result = " & (dblX + dblY)






    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  3. #3
    Join Date
    Oct 1999
    Location
    MD, USA
    Posts
    169

    Re: CInt rounds the number, but i need to display without rounding

    First of all what do you want to do exactly?
    Add just the integer part?
    For e.g if text1 = 3.6 and text2= 4.2
    then Cint(text1) + Cint(text2) will give you 8
    (4 + 4)
    If you just want the integer part to be added i.e 3 + 4 to give 7 then use Fix()



  4. #4
    Join Date
    Oct 1999
    Location
    India
    Posts
    5

    Re: CInt rounds the number, but i need to display without rounding

    why don't you try and use the simple Val function

    for eg.,
    Text1.Text = "4.85"
    Text2.Text = "7.63"

    msgbox val(text1.text) + val(text2.text)
    should give you 12.48



  5. #5
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: CInt rounds the number, but i need to display without rounding

    'Val' is notoriously bad at converting values that aren't numbers into a number equivilent - there was an article in the MSDN a long time ago about the possible errors in using the function.

    For instance, Val("123 Some Road")


    Using the IsNumeric version will work regardless of what's typed in.

    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

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