hi guys,
how can i round the number entered in text boxes
saj
Printable View
hi guys,
how can i round the number entered in text boxes
saj
Hi
VB Code:
Text1.Text = Round(Text1.Text, 3) 'Round(Number, DecimalPlaces)
thank u for replyQuote:
Originally Posted by Andrew G
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
Damn, I thought VB would be smarter than that but oh well. try this
VB Code:
Private Function RoundNumber(Number As Double) As Long txtparts = Split(Number, ".") If UBound(txtparts) > 0 Then If Mid(txtparts(1), 1, 1) >= 5 Then RoundNumber = txtparts(0) + 1 Else RoundNumber = txtparts(0) End If Else RoundNumber = txtparts(0) End If End Function Private Sub Command1_Click() Text1.Text = RoundNumber(Text1.Text) End Sub
But i'm pretty sure there should be a better way, i think :confused:
Your in luck
VB Code:
Text1.Text = Int(Int((Text1.Text + 0.5) * 100) / 100)
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:
Text1.Text = Int(Val(Text1.Text) + 0.5)
casey.
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
thanku andrew and vbasicgirl.....
saj
guys any help on how to generally round up any number to 2 decimal places plz not like round it up or down?