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
Printable View
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
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)
Attachment 134327
but how can I do it in VB.net
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.
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
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.
IF you are going UP to the next .05 ... which isn't rounding... try this:
https://dotnetfiddle.net/06VzyfCode: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
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
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.
Thank you very much, your answer is very clear and I understand it
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
That's it, well done :thumb: