Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding of Numbers in VB 1

Status
Not open for further replies.

sonper

Programmer
Oct 4, 2001
94
0
0
PH
Hi guys!

I wonder if there's a way or function in VB6 that I could use to round off numbers? I am trying to state in nearest multiple of 10 as 950 instead of 948 or 1000 instead of 1004 but it seems that the round() function does not work here. I'll be dealing with amounts so there's an unlimited number of digits involved. Anyone? Please
 
Heres a function to round values as required, its used for money but will work for integers.

example
lv_Result = f_RoundMe(948, 10)
returns 950

Public Function f_RoundMe(fc_Price As Currency, fc_RoundTo As Currency) As Currency

Dim lv_Decimals As Variant
Dim ll_X As Long
Dim lv_X As Variant

lv_X = fc_Price / fc_RoundTo
ll_X = Int(lv_X)
lv_Decimals = CDec(lv_X) - li_X

f_RoundMe = IIf(lv_Decimals >= 0.5, fc_RoundTo * (ll_X + 1), fc_RoundTo * ll_X)

End Function



cjw

 
I personally don't know of one but I'm guessing your primary concern here is not having to write a lot of conditionals. One way to accomplish your task is to take an approach sort of like that used to round numbers I will describe it in pseudo code and then in real VB code. I believe it is correct but I have a few beers in me so if there is a gotchya let me know I will help you later if you need it. :)

Start off with the 10th place(e = 1, 10^e)
Divide the number by 10^e then subtract the floor of this number from the number.
With the remainder, you determine whether or not you need to add one to the (10^e+1)th place(next place up) by whether or not the current digit is over your rounding threshold(above .5) in VB.
Simply repeat this process for each 10th place of your number until the result is evenly divisble by 10(Number Mod 10 = 0). This way it will work with any sized number. It seems to work fine for all the numbers I tried but I could be wrong.

So 210034 will be 210030, and 210035 will be 210030(because VB rounds .5 down) and 210036 would be 210040. Hope this at least gives you a new approach if it doesn't answer your question. Good Luck.


Private Sub Command1_Click()
Dim Xin As Double, Xout As Double, e As Double, R As Double

Xin = 210034
e = 1

Do

Xout = Xin / (10 ^ e)
R = Round(Xout - Int(Xout))
Xout = (Int(Xout) * 10 ^ e) + ((10 ^ R) * R)

Loop Until Xout Mod 10 = 0

Me.Print Xout

End Sub
 
like this is what i mean.

Private Sub Command1_Click()
Dim Xin As Double, Xout As Double, e As Double, R As Double

Xin = 210034
e = 1
Xout = Xin

Do

Xout = Xout / (10 ^ e)
R = Round(Xout - Int(Xout))
Xout = (Int(Xout) * 10 ^ e) + ((10 ^ R) * R)
e=e+1

Loop Until Xout Mod 10 = 0

Me.Print Xout

End Sub
 
Try

Private Sub command1_Click()
MsgBox RoundMultiple(948, 10)
End Sub
Function RoundMultiple(InNumber As Long, Multiple As Long) As Long
RoundMultiple = ((InNumber + (Multiple / 2)) \ Multiple) * Multiple
End Function
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
You are going to complex here.

to round to the nearest 10, use

x = round(y/10) * 10

If i remember correctly, you can also use round(x,-2)
 
You cannot use round(x,-2), but you can write your own function:
------------------------------
Private Function JRound(V As Double, D As Integer) As Double
JRound = Round(V / 10 ^ D) * (10 ^ D)
End Function
------------------------------
Note that I have swapped the signs so that
JRound(1234.56,-1) = 1234.6
JRound(1234.56,1) = 1230
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Inwoner and sunaj:

Ah, there may be a little more to it than just that....unless you are using the functions to do mathematical rounding, and not financial rounding, which is what is probably desired here. If financial rounding is desired, then the VB Round function is of no use.

Try:
Round these to the nearest 10
1235
1245

Round these to the nearest 1/10
1234.56
1234.46 [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

>Round these to the nearest 1/10
1234.56
1234.46

Sorry, wrong example given:

Round these to the nearest 1/10
1234.55
1234.45 [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

Also, for your testing purposes when creating your round function, test these two also:

Round these to the nearest 1/10
1234.55
123.55

See thread222-376571 to see if the function pasts the test. [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Yes yes I know. So use

------------------------
Private Function JRound(V As Double, D As Integer) As Double
JRound = Format(V / 10 ^ D, "0") * (10 ^ D)
End Function
------------------------
If you prefer the statistical offset you get by always rounding 5 upwards. [pipe] Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
hi sonper,
try this code. place one text box and command button in your form.

Private Sub Command1_Click()
Text1.Text = Round(Text1.Text / 10) * 10
End Sub
 
The problem (or not depends on point of view) with the built in round function is the way it handles 5 as compared to 'conventional' rounding

round(125,1) gives 120 so does round(115,1)

ie if the precding digit is odd 5 is rounded up, if even its rounded down.


cjw
 
Hi guys thanks for all your insights. It appeared to me that the round() function does not work here just like what SonOfEmidec1100 has pointed out on the way it (the built-in round function) handles 5.

SonOfEmidec1100,

f_RoundMe(948, 10) gives 950 'correct
f_RoundMe(1004, 10) gives 1010 'should give 1000

JohnYingling,
Thanks! Your RoundMultiple function did all the trick!



 
SonOfEmidec1100 is explaining the stupidity of the actual Round function in VB
in their infinate wisdom (they may have a reason) the function does not always round .5 up in which it should
As he says if it the Integer part of the number is odd then .5 will round up if it is even then it will round down or another way to look at it.
Round(n) will always round the decimal value of .5 to a even number. Which means if you do something like

Round(n/10)*10 in most cases you'll get your answer correct but in that middle case of 5,25,45,65,... you'll get an incorrect answer.
 
<SonOfEmidec1100 is explaining the stupidity of the actual Round function in VB>
It's actually by design! A lot of rounding is done for statistical analysis, and VB round function uses the correct rules for statistical analysis, that is:
< if the precding digit is odd 5 is rounded up, if even its rounded down.>
As always Microsoft is blamed for not being able to guess what you actually want. If it's right for statisticians it's wrong for accountants and vice versa.

However it's very clearly documented in Help! See:
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top