Results 1 to 9 of 9

Thread: [RESOLVED] Rounding numbers in text box

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    278

    Resolved [RESOLVED] Rounding numbers in text box

    hi guys,

    how can i round the number entered in text boxes


    saj

  2. #2
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Rounding numbers in text box

    Hi
    VB Code:
    1. Text1.Text = Round(Text1.Text, 3)   'Round(Number, DecimalPlaces)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    278

    Re: Rounding numbers in text box

    Quote Originally Posted by Andrew G
    Hi
    VB Code:
    1. Text1.Text = Round(Text1.Text, 3)   'Round(Number, DecimalPlaces)
    thank u for reply

    i want to round the textboxes without any decimal part there. if i entered eg:- 12.5 it should be 13, if it s 12.4 it should be 12 only . i use the Round() function as u said, but not working properly

    i tried like this.... round(textbox, 0)


    saj

  4. #4
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Rounding numbers in text box

    Damn, I thought VB would be smarter than that but oh well. try this
    VB Code:
    1. Private Function RoundNumber(Number As Double) As Long
    2. txtparts = Split(Number, ".")
    3. If UBound(txtparts) > 0 Then
    4.     If Mid(txtparts(1), 1, 1) >= 5 Then
    5.         RoundNumber = txtparts(0) + 1
    6.     Else
    7.         RoundNumber = txtparts(0)
    8.     End If
    9. Else
    10.     RoundNumber = txtparts(0)
    11. End If
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15. Text1.Text = RoundNumber(Text1.Text)
    16. End Sub

    But i'm pretty sure there should be a better way, i think

  5. #5
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Rounding numbers in text box

    Your in luck
    VB Code:
    1. Text1.Text = Int(Int((Text1.Text + 0.5) * 100) / 100)

  6. #6
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Rounding numbers in text box

    The reason for the rounding working as it does is because it uses bankers rounding, any number which is #.5 is rounded up if the # is odd and down if the # is even. Andrew G as it if you always want to round up on the .5 but i dont think theres any need for the *100 / 100
    VB Code:
    1. Text1.Text = Int(Val(Text1.Text) + 0.5)

    casey.

  7. #7
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Rounding numbers in text box

    What'd you know, it works without the 100's. The code is actually for calculating interest or something like that, and i just changed some small stuff and tested it and it worked so i didn't bother to check if i needed the 100's.

    Thanks for pointing that out

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    278

    Re: [RESOLVED] Rounding numbers in text box

    thanku andrew and vbasicgirl.....

    saj

  9. #9
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Re: [RESOLVED] Rounding numbers in text box

    guys any help on how to generally round up any number to 2 decimal places plz not like round it up or down?

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