-
Jun 9th, 2014, 05:32 AM
#1
Thread Starter
Member
how to round up values to nearest 50s in vb.net
hi all
i want to round up values to nearest 50s.
for example: 1033 to 1050, 1071 to 1100, 1377 to 1400
how can we do it.
please guide me
thanks in advance
gvg
-
Jun 9th, 2014, 06:19 AM
#2
Re: how to round up values to nearest 50s in vb.net
Take the integer division of your value (value\50). This gives the integer part of the division. Add 1, then multiply by 50.
Be careful. This will wrongly round up for example 100 =>150, but correctly round up 99=>100 and 101=>150
There are various ways to check for this but MOD should help a lot for checking
Last edited by Españolita; Jun 9th, 2014 at 06:23 AM.
-
Jun 9th, 2014, 08:12 AM
#3
Thread Starter
Member
Re: how to round up values to nearest 50s in vb.net
hi espanolita
thanks for your quick reply. what u have told the same thing i have done. but it is not working correctly. this is my code.
**************************
method 1:
Dim a1 As Integer
a1 = side / 50
If CInt(a1) Mod 2 = 0 Then
side = ((a1 + 1) * 50)
Else
side = ((a1) * 50)
End If
*****************************
method 2:
Dim a1 As Integer
a1 = side / 50
side = ((a1 + 1) * 50)
*********************************
some values 1st one working fine but not some. the second one also same.
check for the values:
actual --- required
1033 ---- 1050
1216 ---- 1250
1378 ---- 1350
1525 ---- 1550
1660 ---- 1700
one method is working correctly for some values and the other not working and vice versa.
please guide me.
gvg
-
Jun 9th, 2014, 08:45 AM
#4
Re: how to round up values to nearest 50s in vb.net
Take your original number MOD 50. Then if the result is less than the original number, add 50.
BB
-
Jun 9th, 2014, 09:29 AM
#5
Thread Starter
Member
Re: how to round up values to nearest 50s in vb.net
hi bb
i did not get it. if i take 1033 mod 50 =33
further i could not understand. please clarify it.
-
Jun 9th, 2014, 09:56 AM
#6
Re: how to round up values to nearest 50s in vb.net
Why would 1071 round to 1100? What would 1 round to?
-
Jun 9th, 2014, 10:04 AM
#7
Re: how to round up values to nearest 50s in vb.net
Originally Posted by gvgbabu
hi bb
i did not get it. if i take 1033 mod 50 =33
further i could not understand. please clarify it.
oops, I was a bit dozy. I meant:
Take your original number MOD 50. If the result is more than 0, subtract it from the original number and add 50.
This doesn't apply if the original number is negative.
BB
-
Jun 9th, 2014, 10:10 AM
#8
Thread Starter
Member
Re: how to round up values to nearest 50s in vb.net
thank you very much. Now i got it.
gvg
-
Jun 9th, 2014, 11:41 AM
#9
Re: how to round up values to nearest 50s in vb.net
I usually make sure the number exceeds the next round point, and then round down.
Assume the value we want to round up to is a multiple of "x", then force the number to round to "x", by adding "x-1", and then subtracting the result mod x.
An example function.
Code:
'
Function RoundUpToX(Value As Integer, X As Integer) As Integer
RoundUpToX = Value + X - 1
RoundUpToX -= RoundUpToX Mod X
End Function
p.s. In regards to Espanolita's post, he said to do an Integer divide, i.e. Value \ X. In your post 3, you did floating point divides Value / X.
The Integer divide Value \ X will truncate the floating point value. The floating point divide will round up or down, not truncate.
Last edited by passel; Jun 9th, 2014 at 11:47 AM.
-
Jun 9th, 2014, 12:18 PM
#10
Addicted Member
Re: how to round up values to nearest 50s in vb.net
This is how I did it, and it works:
Code:
Dim number As Decimal
Decimal.TryParse(TextBox1.Text, number)
Dim difference As Decimal
Dim myDifference As Decimal
Dim result As Decimal
difference = CDec((number / 100))
myDifference = CDec(Fix(number / 100))
result = (difference - myDifference) * 100
Select Case result
Case Is < 50
number = number - result + 50
Case Is > 50
number = number - result + 100
End Select
number = number.ToString("N0")
-
Jun 9th, 2014, 12:39 PM
#11
Addicted Member
Re: how to round up values to nearest 50s in vb.net
I used Mod, as boops boops suggested and It works fine.
Code:
Dim number As Decimal
Decimal.TryParse(TextBox1.Text, number)
Dim difference As Decimal
difference = number Mod 50
Select Case difference
Case Is < 50
number = number - difference + 50
Case Is > 50
number = number - difference + 100
End Select
-
Jun 9th, 2014, 04:00 PM
#12
Re: how to round up values to nearest 50s in vb.net
Originally Posted by Atenk
I used Mod, as boops boops suggested and It works fine.
Code:
Dim number As Decimal
Decimal.TryParse(TextBox1.Text, number)
Dim difference As Decimal
difference = number Mod 50
Select Case difference
Case Is < 50
number = number - difference + 50
Case Is > 50
number = number - difference + 100
End Select
That isn't quite what boops boops said.
He said if the remainder was greater than 0 then do the subtraction and add 50. (post #7).
A positive number MOD 50 will return a number in the range of 0 to 49, so your first Case will always be true, and the second case will never occur.
Since zero is less than 50, the code you gave will still add 50 to a number that is already a multiple of 50, so 100 will become 150, when it shouldn't. That is the reason boops boops said to specifically test for > 0 (i.e. not 0).
You can modify your code to do that
Code:
'
Dim number As Decimal
Decimal.TryParse(TextBox1.Text, number)
Dim difference As Decimal
difference = number Mod 50
Select Case difference
Case Is > 0
number = number - difference + 50
End Select
But of course since you only need to test a single value, you don't need a case statement
so this is more along the lines of what boops boops suggested.
Code:
'
Dim number As Decimal
Decimal.TryParse(TextBox1.Text, number)
Dim difference As Decimal = number Mod 50
If difference > 0 Then
number = number - difference + 50
End If
Which may be more efficient than mine, I'm not sure,
because of the difference being, being able to avoid the - and +, but at the cost of a conditional branch.
Using your code with my usual way of doing it, in-lined here instead of a function, so it is directly comparable to the above two code examples.
Code:
Dim number As Decimal
Decimal.TryParse(TextBox1.Text, number)
number += 49
number -= number Mod 50
Last edited by passel; Jun 9th, 2014 at 04:08 PM.
-
Jun 9th, 2014, 04:13 PM
#13
Addicted Member
Re: how to round up values to nearest 50s in vb.net
I've just taken "MOD" from boops boops without paying attention to the rest, sorry.
Thanks for the remark; the second bit of code won't work without that condition: "if remainder was greater than zero".
Last edited by Atenk; Jun 9th, 2014 at 04:24 PM.
-
Jun 9th, 2014, 05:17 PM
#14
Re: how to round up values to nearest 50s in vb.net
How about:
Code:
Function RoundUpToNearest(Value As Integer, nearest As Integer) As Integer
Return CInt(Math.Ceiling(Value / nearest) * nearest)
End Function
-
Jun 9th, 2014, 08:03 PM
#15
Re: how to round up values to nearest 50s in vb.net
It looks like a good one, especially since it is a one liner. But it is doing a floating point divide and floating point function (Math.Ceiling).
Since the Mod function is an Integer divide, and then you have an Integer - and +, I would expect the integer math based functions to be quicker by a good margin, although unless you're doing millions of these things, it would not be apparent to the user.
boops boops is also using Integers, so out of curiosity, I went ahead and ran 100 million calls to the functions (passing the numbers 1 to 100000000 in a loop to the functions).
Did each run three times, from the IDE with no optimizations selected (x86 code on win7-64bit) and timed with a stop watch object.
The results (in milliseconds for 100 million calls):
boops boops: 2044, 2073, 2062
passel: 1868, 1919, 1891
dbasnett: 6038, 6032, 6023
The calling code
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim t As Integer
Dim sw As New Stopwatch
sw.Start()
For i As Integer = 1 To 100000000
t = RoundUpToX(i, 50)
Next
sw.Stop()
Debug.Print(sw.ElapsedMilliseconds.ToString)
End Sub
And each implementation of the functions as tested.
'Based on boops boops suggestion
Code:
Function RoundUpToX(Value As Integer, round As Integer) As Integer
RoundUpToX = Value
Dim difference As Integer = Value Mod 50
If difference > 0 Then
RoundUpToX = RoundUpToX - difference + 50
End If
End Function
'dbasnett's, (name changed to match calling code)
Code:
Function RoundUpToX(Value As Integer, nearest As Integer) As Integer
Return CInt(Math.Ceiling(Value / nearest) * nearest)
End Function
'passel's
Code:
Function RoundUpToX(Value As Integer, round As Integer) As Integer
RoundUpToX = Value + round - 1
RoundUpToX -= RoundUpToX Mod round
End Function
p.s. I realized when I added more information above about the OS that I'm running on Win7-64 but default to compiling for x86 because I like the interactive modification of code as I debug capability.
I changed the option to compile for x64, and that brought dbasnett's function much closer to the Integer functions, and boops boops based code using the If condition actually pulled slightly ahead to lead the pack.
boops boops: 1185, 1153, 1162
passel: 1279, 1287, 1293
dbasnett: 2140, 2107, 2096
Last edited by passel; Jun 9th, 2014 at 08:17 PM.
-
Jun 9th, 2014, 08:37 PM
#16
Addicted Member
Re: how to round up values to nearest 50s in vb.net
Just one little remark: the number, or math.abs(number) must be greater than zero; otherwise 50 will be added to zero.
Last edited by Atenk; Jun 9th, 2014 at 09:09 PM.
-
Jun 10th, 2014, 06:33 AM
#17
Re: how to round up values to nearest 50s in vb.net
How do they compare if the number being rounded is negative? Do they all produce the correct results?
-
Jun 10th, 2014, 07:19 AM
#18
Addicted Member
Re: how to round up values to nearest 50s in vb.net
If the starting number is negative, the result will be always zero. But if that starting number is zero, then 50 will be added.
-
Jun 10th, 2014, 09:27 AM
#19
Re: how to round up values to nearest 50s in vb.net
Originally Posted by dbasnett
How do they compare if the number being rounded is negative? Do they all produce the correct results?
I guess it depends on the definition of correct.
From a strictly mathematical definition, then only the Math.Ceiling version will round up to a higher multiple of 50.
But, most of the uses of this function that I've come across had to do with sales of small part items that are fairly inexpensive on a piece level.
Often, those parts, such as nails, are sold by weight, but in other cases, perhaps screws, bolts, etc, they are priced by lots of 5 or 10 or more pieces, so when a need for X amount of such item are needed, the order is rounded up to the next lot size for pricing and delivery.
In that case, buying 0 items or a negative number of items is not applicable, so should be caught at the numeric input stage and never be submitted to the function.
-
Jun 10th, 2014, 10:20 AM
#20
Re: how to round up values to nearest 50s in vb.net
Originally Posted by passel
I guess it depends on the definition of correct.
From a strictly mathematical definition, then only the Math.Ceiling version will round up to a higher multiple of 50.
But, most of the uses of this function that I've come across had to do with sales of small part items that are fairly inexpensive on a piece level.
Often, those parts, such as nails, are sold by weight, but in other cases, perhaps screws, bolts, etc, they are priced by lots of 5 or 10 or more pieces, so when a need for X amount of such item are needed, the order is rounded up to the next lot size for pricing and delivery.
In that case, buying 0 items or a negative number of items is not applicable, so should be caught at the numeric input stage and never be submitted to the function.
If that is the use then you are correct. Only the OP knows.
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
|