|
-
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
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
|