|
-
Sep 14th, 2005, 11:09 AM
#1
Thread Starter
Member
Rounding to nearest 25,000
This is not an ordinary rounding issue. Here's what I need to do. I have to round any number up to the nearest 25,000 increment.
For example:
124,698 would need to round to 125,000
126,125 would need to round to 150,000
1,278,189 would need to round to 1,300,000
Anyone know of a mathematical formula for doing this? Or any good ideas as to how to accomplish this? THANKS!
-
Sep 14th, 2005, 11:26 AM
#2
Re: Rounding to nearest 25,000
VB Code:
MsgBox YourValue + 25000 - YourValue Mod 25000
-
Sep 14th, 2005, 11:30 AM
#3
Re: Rounding to nearest 25,000
Use an integer division (same as regular division but use \ instead of /). This will ALWAYS round down.
You'll need to add 1 (to make it a round up) unless it divided exactly (cos then you shouldn't be rounded at all). You check that by using Mod.
So:-
[Highlight=VB]
answer = x\25000
if answer Mod 25000 > 0 then
answer = answer + 1
end if
-
Sep 14th, 2005, 11:35 AM
#4
Re: Rounding to nearest 25,000
Something like this:
(replace with appropriate variables)
VB Code:
Msgbox Round(124698 /1000) * 1000
Pradeep
-
Sep 14th, 2005, 11:35 AM
#5
Re: Rounding to nearest 25,000
My solution does not work properly if decimals are involved.
-
Sep 14th, 2005, 11:37 AM
#6
Re: Rounding to nearest 25,000
 Originally Posted by Pradeep1210
Something like this:
(replace with appropriate variables)
VB Code:
Msgbox Round(124698 /1000) * 1000
Pradeep 
That doesn't work. eg 134998 -> 135000
-
Sep 14th, 2005, 11:53 AM
#7
Re: Rounding to nearest 25,000
If you want 250 to round down, just add 1 to t
VB Code:
Option Explicit
Private Sub Form_Load()
Dim x As Long
Dim t As Long
x = Val(InputBox("Enter a Number"))
Do While x <> 0
t = (x \ 25000) * 25000
if t=0 then t=t+ 1 ' if you want it rounded up
MsgBox t
x = Val(InputBox("Enter a Number"))
Loop
End
End Sub
-
Sep 14th, 2005, 12:06 PM
#8
Re: Rounding to nearest 25,000
 Originally Posted by dglienna
If you want 250 to round down, just add 1 to t
VB Code:
Option Explicit
Private Sub Form_Load()
Dim x As Long
Dim t As Long
x = Val(InputBox("Enter a Number"))
Do While x <> 0
t = (x \ 25000) * 25000
if t=0 then t=t+ 1 ' if you want it rounded up
MsgBox t
x = Val(InputBox("Enter a Number"))
Loop
End
End Sub
Did someone ask how to round down?
-
Sep 14th, 2005, 12:10 PM
#9
Re: Rounding to nearest 25,000
Using my method, 250 would be the nearest 25000, which would be zero
If he wanted it like that, he'd have to remove the line.
Numbers up to 24999 would show up as 0.
If he wants 24999 to point to 25000, then I'd have to rewrite some code.
My code just finds number of times 25000 goes into the number.
EDIT: This seems to work
VB Code:
Option Explicit
Private Sub Form_Load()
Dim x As Long
Dim t As Long
x = Val(InputBox("Enter a Number"))
Do While x <> 0
t = (x \ 25000) * 25000
If Abs(x - t) > 12500 Then
t = t + 25000
End If
MsgBox t
x = Val(InputBox("Enter a Number"))
Loop
End
End Sub
But it still rounds 500 down to 0, and I wasn't sure if thats what the poster wanted.
Last edited by dglienna; Sep 14th, 2005 at 12:19 PM.
-
Sep 14th, 2005, 12:11 PM
#10
Re: Rounding to nearest 25,000
thumbs up ML............i was abt to post the same thing
-
Sep 14th, 2005, 12:22 PM
#11
Re: Rounding to nearest 25,000
"Using my method, 250 would be the nearest 25000, which would be zero" ==> What does that mean?
My code just finds number of times 25000 goes into the number.==> No it doesn't.
-
Sep 14th, 2005, 12:24 PM
#12
Re: Rounding to nearest 25,000
 Originally Posted by dglienna
Using my method, 250 would be the nearest 25000, which would be zero
If he wanted it like that, he'd have to remove the line.
Numbers up to 24999 would show up as 0.
If he wants 24999 to point to 25000, then I'd have to rewrite some code.
My code just finds number of times 25000 goes into the number.
Well, since the OP said
 Originally Posted by CJD#11
I have to round any number up to the nearest 25,000 increment.
And
 Originally Posted by CJD#11
124,698 would need to round to 125,000
I would assume that 24999 would point to 25000, as would 250. Or even 1 for that matter. The only time the result should be 0 is if the value is 0 to begin with. At 25001, it becomes 50000. But your solution would still work, by adding 1 to t as you mentioned.
-tg
-
Sep 14th, 2005, 12:48 PM
#13
Re: Rounding to nearest 25,000
Hmm. I read it before I went out, and posted when I got back. Must have missed that. Thanks.
-
Sep 14th, 2005, 12:56 PM
#14
Re: Rounding to nearest 25,000
 Originally Posted by MartinLiss
That doesn't work. eg 134998 -> 135000
That example rounded it off to nearest 1000.
Try this:
VB Code:
Private Sub Command1_Click()
Dim nNum As Long, nRound As Long
nRound = 25000
nNum = InputBox("Enter Number:")
MsgBox Round(nNum / nRound) * nRound
End Sub
Pradeep
-
Sep 14th, 2005, 01:00 PM
#15
Addicted Member
Re: Rounding to nearest 25,000
i think which decimal place you want to round is important, first this must be decided then calculation performed.
for example if you want to round 124,698 to 125,000
firstly you must divide number by 1000 and round it
secondly zeros needed must be added
You can do while you think that you can do
If you think my answer solve your question, please rate it.
-
Sep 14th, 2005, 01:01 PM
#16
Re: Rounding to nearest 25,000
 Originally Posted by FunkyDexter
Use an integer division (same as regular division but use \ instead of /). This will ALWAYS round down.
You know what? That's not true. The integer division operator will round to the nearest integer, not always round down.
-
Sep 14th, 2005, 01:12 PM
#17
Re: Rounding to nearest 25,000
 Originally Posted by Pradeep1210
That example rounded it off to nearest 1000.
Try this:
VB Code:
Private Sub Command1_Click()
Dim nNum As Long, nRound As Long
nRound = 25000
nNum = InputBox("Enter Number:")
MsgBox Round(nNum / nRound) * nRound
End Sub
Pradeep 
No, sorry 35000 -> 25000
-
Sep 14th, 2005, 01:15 PM
#18
Re: Rounding to nearest 25,000
 Originally Posted by MartinLiss
"Using my method, 250 would be the nearest 25000, which would be zero" ==> What does that mean?
My code just finds number of times 25000 goes into the number.==> No it doesn't.
The first example was rounding down to the nearest 25000
Using my method, 250 goes into 25000, zero times
My original code just found number of times a number goes into 25000 if it is over 25000
My explanatiion was a bit off. Sorry
-
Sep 14th, 2005, 01:16 PM
#19
New Member
Re: Rounding to nearest 25,000
Here, try this...
VB Code:
Private Function getRnd(ByRef lNumIn As Long, lRnd As Long) As Long
Dim lNew As Long
Dim lTmp As Long
On Error GoTo Err_H
'lNumIn = Number to Round off
'lRnd = Control Variable
lTmp = lNumIn Mod lRnd
If lTmp < (lRnd / 2) Then
lNew = lNumIn - lTmp
Else
lNew = lNumIn + (lRnd - lTmp)
End If
getRnd = lNew
Exit Function
Err_H:
getRnd = -1
End Function
I hope I'm on the right track for you.
-
Sep 14th, 2005, 01:19 PM
#20
Re: Rounding to nearest 25,000
 Originally Posted by MartinLiss
No, sorry 35000 -> 25000
That's right.
35000 is nearer to 25000 than 50000
The midpoint is 37500. So after this it will round to 50000
Pradeep
-
Sep 14th, 2005, 01:21 PM
#21
Re: Rounding to nearest 25,000
But as I pointed out to dglienna, the poster wants to round UP.
-
Sep 14th, 2005, 01:23 PM
#22
Re: Rounding to nearest 25,000
I don't get it. Why are you all givin new complex answers when Marty showed such a simple brilliant solution in the first reply?
-
Sep 14th, 2005, 01:37 PM
#23
Re: Rounding to nearest 25,000
I have this theory that I think has just been further proven by this very thread:
Ask 10 developers how to do something and you will get 23 different opinions.
-tg
-
Sep 14th, 2005, 01:41 PM
#24
Thread Starter
Member
Re: Rounding to nearest 25,000
 Originally Posted by Joacim Andersson
I don't get it. Why are you all givin new complex answers when Marty showed such a simple brilliant solution in the first reply?
Really? I don't see how that would work. And yes, all numbers(regardless of decimal places, which would be 2 b/c all numbers are monetary values) have to be rounded up to the next 25000 increment.
-
Sep 14th, 2005, 01:41 PM
#25
Re: Rounding to nearest 25,000
 Originally Posted by dglienna
The first example was rounding down to the nearest 25000
Using my method, 250 goes into 25000, zero times
My original code just found number of times a number goes into 25000 if it is over 25000
My explanatiion was a bit off. Sorry 
You mean 25000 goes into 250 0 times.... 250 will go into 25000 100 times....
So the logic should be to see how many times 25000 will go into a number....
The integer division operator will round to the nearest integer, not always round down. -- actualy it goes to the nearest number, unles it's at the break point, in which case it goes to the nearest EVEN integer.
So Round(1.5) gives you 2, but Round(2.5) also gives 2..... I don't know how many times that caused us greif in our app.
-tg
-
Sep 14th, 2005, 01:43 PM
#26
Re: Rounding to nearest 25,000
 Originally Posted by CJD#11
Really? I don't see how that would work. And yes, all numbers(regardless of decimal places, which would be 2 b/c all numbers are monetary values) have to be rounded up to the next 25000 increment.
VB Code:
MsgBox Int(YourNumber) + 25000& - Int(YourNumber) Mod 25000&
EDIT: That works unless your number is negative in which case you need to subtract 25000 first.
VB Code:
If YourNumber < 0 Then
YourNumber = YourNumber - 25000&
End If
MsgBox Int(YourNumber) + 25000& - Int(YourNumber) Mod 25000&
Last edited by Joacim Andersson; Sep 14th, 2005 at 01:46 PM.
-
Sep 14th, 2005, 01:46 PM
#27
Thread Starter
Member
Re: Rounding to nearest 25,000
 Originally Posted by techgnome
So Round(1.5) gives you 2, but Round(2.5) also gives 2..... I don't know how many times that caused us greif in our app.
-tg
Been there, done that!
-
Sep 14th, 2005, 01:52 PM
#28
Re: Rounding to nearest 25,000
 Originally Posted by techgnome
The integer division operator will round to the nearest integer, not always round down. -- actualy it goes to the nearest number, unles it's at the break point, in which case it goes to the nearest EVEN integer.
When I claimed that the integer division operator not always will round down. I was talking about the integer division operator (the backslash) not the Round function. Maybe I should elaborate on that, what I mean is that if not both operands are integers VB will first round the operands to the closest integer number before it does the division in which case it rounds down. So for example 17 \ 3 will be 5, meaning it rounds down. But 17.6 \ 3 will become 6, which means that VB first rounds 17.6 to 18 before it does the division.
-
Sep 14th, 2005, 01:52 PM
#29
Re: Rounding to nearest 25,000
 Originally Posted by MartinLiss
But as I pointed out to dglienna, the poster wants to round UP.
Oh! I missed that UP
Here's the fix:
VB Code:
Private Sub Command1_Click()
Dim nNum As Long, nRound As Long
nRound = 25000
nNum = InputBox("Enter Number:")
If nNum Mod nRound = 0 Then
MsgBox ((nNum \ nRound)) * nRound
Else
MsgBox (1 + (nNum \ nRound)) * nRound
End If
End Sub
-
Sep 14th, 2005, 01:55 PM
#30
Re: Rounding to nearest 25,000
I still think Marty had the best solution. With the simple fix of using the Int function as I showed above it works perfectly.
-
Sep 14th, 2005, 02:00 PM
#31
Thread Starter
Member
Re: Rounding to nearest 25,000
 Originally Posted by Pradeep1210
Oh! I missed that UP
Here's the fix:
VB Code:
Private Sub Command1_Click()
Dim nNum As Long, nRound As Long
nRound = 25000
nNum = InputBox("Enter Number:")
If nNum Mod nRound = 0 Then
MsgBox ((nNum \ nRound)) * nRound
Else
MsgBox (1 + (nNum \ nRound)) * nRound
End If
End Sub
Doing this math using the nubmer 26,000, the end result was 51000, which would not meet my needs. It would need to = 50,000. Or did I look at that wrong?
-
Sep 14th, 2005, 02:02 PM
#32
Re: Rounding to nearest 25,000
 Originally Posted by CJD#11
Doing this math using the nubmer 26,000, the end result was 51000, which would not meet my needs. It would need to = 50,000. Or did I look at that wrong?
To me it gives 50000
Did you copy the code exactly as it is ?
Pradeep
-
Sep 14th, 2005, 02:03 PM
#33
Thread Starter
Member
Re: Rounding to nearest 25,000
O.k., lets back off a little. Forget the whole 25000 thing. How about just a way to round any number UP every time?
Ex 4.1 = 5
4.3 = 5
4.6 = 5
5.1 = 6
From what I've read, there is no VB command to do this, so it has to be 'forced' to happen.
-
Sep 14th, 2005, 02:03 PM
#34
Re: Rounding to nearest 25,000
I still don't get it... CJD#11 did you even try this out?
VB Code:
MsgBox Int(YourNumber) + 25000& - Int(YourNumber) Mod 25000&
-
Sep 14th, 2005, 02:04 PM
#35
Re: Rounding to nearest 25,000
 Originally Posted by CJD#11
O.k., lets back off a little. Forget the whole 25000 thing. How about just a way to round any number UP every time?
Ex 4.1 = 5
4.3 = 5
4.6 = 5
5.1 = 6
From what I've read, there is no VB command to do this, so it has to be 'forced' to happen.
MsgBox Int(theNumber) + 1
-
Sep 14th, 2005, 02:08 PM
#36
Re: Rounding to nearest 25,000
Stupid timeouts..... accidently double posted.....
Plz disregard.
-tg
-
Sep 14th, 2005, 02:10 PM
#37
Thread Starter
Member
Re: Rounding to nearest 25,000
 Originally Posted by Pradeep1210
To me it gives 50000
Did you copy the code exactly as it is ?
Pradeep
AHHHH, o.k. that did work.
I did the math out by hand, not using the code.
-
Sep 14th, 2005, 02:11 PM
#38
Re: Rounding to nearest 25,000
 Originally Posted by Joacim Andersson
MsgBox Int(theNumber) + 1
It should be :
VB Code:
If Int(theNumber) = theNumber Then
MsgBox theNumber
Else
MsgBox Int(theNumber) + 1
End If
Pradeep
-
Sep 14th, 2005, 02:13 PM
#39
Re: Rounding to nearest 25,000
Not if you want to round up every number.
-
Sep 14th, 2005, 02:13 PM
#40
Re: Rounding to nearest 25,000
 Originally Posted by Joacim Andersson
When I claimed that the integer division operator not always will round down. I was talking about the integer division operator (the backslash) not the Round function. Maybe I should elaborate on that, what I mean is that if not both operands are integers VB will first round the operands to the closest integer number before it does the division in which case it rounds down. So for example 17 \ 3 will be 5, meaning it rounds down. But 17.6 \ 3 will become 6, which means that VB first rounds 17.6 to 18 before it does the division.
I was smoking something when I wrote that.... the air has now cleared and I don't know *what* I was thinking.... but what will VB do to 18.5 vs 17.5 before doing int division?
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|