|
-
Jan 16th, 2016, 04:00 PM
#1
Thread Starter
New Member
Rounding value
Hello, m8
I want to know what is the equation which round 7.33 to 7.35 or 7.67 to 7.70
meaning round the 2nd decimal number to the nearest five
Thank you
-
Jan 16th, 2016, 08:23 PM
#2
Re: Rounding value
Firstly, that isn't rounding the second decimal number to the nearest five or else 7.67 would become 7.65. So, do you actually want to round or specifically round up?
Either way, there's no one method to that exactly. You would have to multiply, then round or ceiling, then divide. Exactly how depends on whether you want to round or ceiling.
-
Jan 17th, 2016, 12:21 AM
#3
Thread Starter
New Member
Re: Rounding value
 Originally Posted by jmcilhinney
Firstly, that isn't rounding the second decimal number to the nearest five or else 7.67 would become 7.65. So, do you actually want to round or specifically round up?
Either way, there's no one method to that exactly. You would have to multiply, then round or ceiling, then divide. Exactly how depends on whether you want to round or ceiling.
Thank you for your reply
I want to round up whatever the value of the second decimal number to nearest five (7.01 to 7.05 or 7.333 to be 7.35 and 7.61 to be 7.65) like that not to the least value.
in EXCEL the CEILING function is doing that by Ceiling(cell number , 0.05)

but how can I do it in VB.net
Last edited by mohammedkhairy; Jan 17th, 2016 at 12:33 AM.
-
Jan 17th, 2016, 01:45 AM
#4
Re: Rounding value
In VB.NET you would use the Math.Ceiling method. It only rounds to whole numbers though, so you'll need to multiply first and then divide afterwards.
-
Jan 17th, 2016, 03:40 AM
#5
Thread Starter
New Member
Re: Rounding value
 Originally Posted by jmcilhinney
In VB.NET you would use the Math.Ceiling method. It only rounds to whole numbers though, so you'll need to multiply first and then divide afterwards.
Can you please do an example showing the method
-
Jan 17th, 2016, 07:55 AM
#6
Re: Rounding value
 Originally Posted by mohammedkhairy
Can you please do an example showing the method
Why can't you make an effort to do it for yourself? I've told you what to do. Just do it.
-
Jan 17th, 2016, 11:45 AM
#7
Thread Starter
New Member
Re: Rounding value
 Originally Posted by jmcilhinney
Why can't you make an effort to do it for yourself? I've told you what to do. Just do it.
Thank you, for our effort
but I'm new using VB.NET, what I understand from your answer that I should use MATH.CEILING, which will round the positive value to the next bigger integer number " 7.333 will be 8" .
But I don't understand what do you mean by
 Originally Posted by jmcilhinney
you'll need to multiply first and then divide afterwards
-
Jan 17th, 2016, 12:11 PM
#8
Re: Rounding value
As an example:
If you multiply 7.333 by 100 it becomes 733.3
The Ceiling of that is 734
Divide that by the same as you multiplied by (in this case 100), and the result is 7.34
What you need to do is work out what number to multiply/divide by.
As with many programming problems, this kind of thing is not specific to VB.Net or even to programming.
-
Jan 17th, 2016, 01:01 PM
#9
Thread Starter
New Member
Re: Rounding value
 Originally Posted by si_the_geek
As an example:
If you multiply 7.333 by 100 it becomes 733.3
The Ceiling of that is 734
Divide that by the same as you multiplied by (in this case 100), and the result is 7.34
What you need to do is work out what number to multiply/divide by.
As with many programming problems, this kind of thing is not specific to VB.Net or even to programming.
Thank you,
But it will only round to the bigger value 734 or 761 I need it to be 735 or 765 (to the nearest five)
-
Jan 17th, 2016, 01:54 PM
#10
Re: Rounding value
If you multiply (and divide) by 100, then yes... but that is why I wrote the second half of my post:
 Originally Posted by si_the_geek
What you need to do is work out what number to multiply/divide by.
As with many programming problems, this kind of thing is not specific to VB.Net or even to programming.
While we could do it all for you, that would mean you are less likely to learn/understand.
-
Jan 17th, 2016, 01:57 PM
#11
Thread Starter
New Member
Re: Rounding value
 Originally Posted by si_the_geek
If you multiply (and divide) by 100, then yes... but that is why I wrote the second half of my post:
While we could do it all for you, that would mean you are less likely to learn/understand.
Ok, Thank you
I will try and l hope solve this problem
Thanks for your concern again
-
Jan 17th, 2016, 02:02 PM
#12
Re: Rounding value
IF you are going UP to the next .05 ... which isn't rounding... try this:
Code:
Public Sub Main()
dim someNum as Decimal =7.5151 '5.01
Console.WriteLine(someNum.ToString())
someNum *= 100 '751.51
Console.WriteLine(someNum.ToString())
someNum = Math.Ceiling(someNum) '752\
Console.WriteLine(someNum.ToString())
someNum += (5- ((someNum + 5) mod 5)) '<--- this is the key here
'Console.WriteLine((5-((someNum + 5) mod 5)).ToString)
Console.WriteLine(someNum.ToString())
someNum /= 100
Console.WriteLine(someNum.ToString())
End Sub
https://dotnetfiddle.net/06Vzyf
PArt of the issue is you keep calling it rounding, but that isn't what you're looking for... at any rate, this gives you the results you defined with your excel screen shot. There is a little more to it than just the multiply/divide ... you also need to figure how far off from the next 5 you are. I've done this by adding 5, using mod to get the remainder, then subtracting that from 5 and adding it back to the original number. There might be a way to simplify it.
Using the above, I got 7.55 and 5.05 for the final results.
-tg
-
Jan 17th, 2016, 03:15 PM
#13
Re: Rounding value
There's no need for Mod etc, just picking the right number (which is less than 100, and is the reciprocal of the 'rounding' amount) is enough.
-
Jan 17th, 2016, 03:59 PM
#14
Thread Starter
New Member
Re: Rounding value
 Originally Posted by techgnome
IF you are going UP to the next .05 ... which isn't rounding... try this:
Code:
Public Sub Main()
dim someNum as Decimal =7.5151 '5.01
Console.WriteLine(someNum.ToString())
someNum *= 100 '751.51
Console.WriteLine(someNum.ToString())
someNum = Math.Ceiling(someNum) '752\
Console.WriteLine(someNum.ToString())
someNum += (5- ((someNum + 5) mod 5)) '<--- this is the key here
'Console.WriteLine((5-((someNum + 5) mod 5)).ToString)
Console.WriteLine(someNum.ToString())
someNum /= 100
Console.WriteLine(someNum.ToString())
End Sub
https://dotnetfiddle.net/06Vzyf
PArt of the issue is you keep calling it rounding, but that isn't what you're looking for... at any rate, this gives you the results you defined with your excel screen shot. There is a little more to it than just the multiply/divide ... you also need to figure how far off from the next 5 you are. I've done this by adding 5, using mod to get the remainder, then subtracting that from 5 and adding it back to the original number. There might be a way to simplify it.
Using the above, I got 7.55 and 5.05 for the final results.
-tg
Thank you very much, your answer is very clear and I understand it
 Originally Posted by si_the_geek
There's no need for Mod etc, just picking the right number (which is less than 100, and is the reciprocal of the 'rounding' amount) is enough.
Thanks, I multiply the value with 20 and ceiling it then divided it by 20 and get the answer
Example Code:
Dim x as decimal x = math.ceiling(7.33333*20) textbox?.text = x/20 ' 7.33 >> 7.35
-
Jan 17th, 2016, 05:49 PM
#15
Re: Rounding value
That's it, well done
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
|