 
Moderator
Rounding 1234 rounded to the nearest hundred is 1200.
Easy.
1.27 rounded to the nearest 1/4 or half...
Is there a formula to figure this up?
I've got so far
mod(n/x) >= x/2
....if that's true, then round:
x(int(n/x)) + x
... I think. I'm not sure.
I need to round by numbers that are not whole numbers (like 1/4 and 1/2) and by numbers that are not divisible by 10 (like 2, 4, 6, etc.)
Is there a general rounding formula...somewhere?

WinAmp. Llama Tested. Mother Approved.
Jordan Gottlieb
President, Charlotte Mercury User Group
Qualitech Solutions, Inc. jgottlieb@qualitechsolutions.com Jordan Gottlieb
Senior Consultant, Orasi Software
Twitter: @JG_QA 
Senior Member
Re: Rounding Thought this was a poker thread.
 
Senior Member
Re: Rounding <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by jgottlieb: 1234 rounded to the nearest hundred is 1200.
Easy.
1.27 rounded to the nearest 1/4 or half...
Is there a formula to figure this up?
I've got so far
mod(n/x) >= x/2
....if that's true, then round:
x(int(n/x)) + x
... I think. I'm not sure.
I need to round by numbers that are not whole numbers (like 1/4 and 1/2) and by numbers that are not divisible by 10 (like 2, 4, 6, etc.)
Is there a general rounding formula...somewhere? <HR></BLOCKQUOTE>
Use Excel
=MROUND(calculation,0.5), for a result rounded to the NEAREST 0.5
You can also round to the NEAREST 0.25 (0.5, 0.75, 0.00) the same way:
=MROUND(calculation, 0.25)
 Life should NOT be a trip to the grave with the intention of arriving safely in an cool and well preserved body, but rather to skid in, chocolate in one hand, beer in the other, body wrecked, totally worn out and screaming WOO HOO what a ride! 
Senior Member
Re: Rounding (int((orig_nbr / round) + 0.5)) * round
For instance, to round 123.45 to nearest .25:
rounded = (int((123.45 / .25) + 0.5)) * .25
Here's the Perl script I used to test this:

Charles Reace
charlesDOTreaceATverizonDOTnet
"Any sufficiently advanced bug is indistinguishable from a feature." Bruce Brown web site  [url=http://www.ebookworm.us/[/url]
[i]...Sound trumpets! Every trumpet in the host! / Sixty thousand, on these words, sound, so high the mountains sound, and the valleys resound.</i] (The Song of Roland) 
Moderator
Re: Rounding Corklad, I used Excel to determine the logic. It's going into code, so a spreadsheet won't work....
Here's what I came up with:
if mod(n/x) >= x/2
true: round up
(x * int(n/x)) + x
false: round down
x * int(n/x)
Charles says:
<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>rounded = (int((123.45 / .25) + 0.5)) * .25<HR></BLOCKQUOTE>
Mine: (.25 * int(123.45/.25)) + .25
Yours: (int((123.45 / .25) + 0.5)) * .25
Shouldn't you adjust up (+.25) after you get your base (.25*int)?

WinAmp. Llama Tested. Mother Approved.
Jordan Gottlieb
President, Charlotte Mercury User Group
Qualitech Solutions, Inc. jgottlieb@qualitechsolutions.com Jordan Gottlieb
Senior Consultant, Orasi Software
Twitter: @JG_QA 
Senior Member
Re: Rounding <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Mine: (.25 * int(123.45/.25)) + .25
Yours: (int((123.45 / .25) + 0.5)) * .25
Shouldn't you adjust up (+.25) after you get your base (.25*int)?<HR></BLOCKQUOTE>
Nope. The "+ 0.5" is so that the int() function will round to the nearest whole number of times that the rounding number can be divided into the original number, otherwise all values would always round down.

Charles Reace
charlesDOTreaceATverizonDOTnet
"Any sufficiently advanced bug is indistinguishable from a feature." Bruce Brown web site  [url=http://www.ebookworm.us/[/url]
[i]...Sound trumpets! Every trumpet in the host! / Sixty thousand, on these words, sound, so high the mountains sound, and the valleys resound.</i] (The Song of Roland)
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 