CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: math

  1. #1
    Join Date
    Apr 1999
    Posts
    108

    math

    lets say i have a text box that contains

    123223423.23423234

    is there a way to assign a variable (lets say A) to be a rounded up # of the textbox?

    basically..how do i get rid of the "." and just store the rounded up #

    ?

  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: math

    Hi,

    If you are within the range of 'Long' (2,147,483,647) you can use
    CLng(Text1.text) to convert the text to long and this conv. is "rounded to nearest whole no (sign incl)".

    If you are outside that range, no equil for int64 in VB ( 5.0 atleast, i dont know abt 6.0).
    So you have to go for Decimal/Currency which have limited no. of decimal pts. above them you have to go for Single/Double. For display, make good use for Format fn, so that for all 'practical purposes' it looks like a long-long int:-)

    Or if you have the courage, go down to CRT functions ( 'C'-Runtime & WinAPI) that support int64.

    Ravi


  3. #3
    Join Date
    Apr 1999
    Posts
    108

    Re: math

    unfortunately, that doesnt answer my question just yet ..u took is far beyond what i needed

    basically i have a text1.text containing the intergers 12345.1234

    since 12345 rounded up or down is still 12345 then that what i want to return as the value

    i think i thas something to do with absolute value..

    eg.. abs(text1.text)

    but for some reaosn i cant get it to work?

    -joe

    ?

  4. #4
    Join Date
    Apr 1999
    Posts
    108

    Re: math

    ennvermind thanks anyways
    i got it

    int(text1.text)

    =)>

    ?

  5. #5
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: math

    Mind you, i used the phrase - "rounded to nearest whole no (sign incl)".

    If you come from 'C' bkgrnd there are 'floor' and 'ceil' functions. 'Int' in Vb is similar to 'floor'.

    So, int(123.123) = int(123.523) = 123 i.e it always removes the fractional part and gives
    the whole no.
    while cint(123.123) = 123 and cint(123.523) = 124 and also CInt(123.5000) = 124.

    For negetive nos int() gives lot of trouble.
    int(-123.1) gives -124 and not -123 as we normally expect.!!, while Cint gives -123

    Hence Rounding Up or down is not same as rounding to nearest integer!!

    Ravi


  6. #6
    Join Date
    May 1999
    Posts
    7

    Re: math

    CInt also has some "unexpected" behaviour:

    CInt(2.5) = 2
    CInt(1.5) = 2

    Int(2.5) = 2
    Int(1.5) = 1

    I'm told that this behaviour is documented, but that doesn't make it "expected".


  7. #7
    Join Date
    Apr 1999
    Posts
    108

    Re: math

    yeah i foudn i had the same problem.. but i managed to fix it

    thanks again tho

    ?

  8. #8
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: math

    Use this, to get the nearest-integer always:

    int(val + 0.5)


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