1. ## 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

2. ## Re: Rounding

Thought this was a poker thread.

------------------

3. ## 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)

------------------

4. ## 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

5. ## 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

------------------
WinAmp. Llama Tested. Mother Approved.
Jordan Gottlieb
President, Charlotte Mercury User Group
Qualitech Solutions, Inc.
jgottlieb@qualitechsolutions.com

6. ## 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

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.