Round to ANY value equation,, ie..25-50-100-13 anything
Round to ANY value equation,, ie..25-50-100-13 anything
(OP)
This may help someone. I recently developed a spreadsheet that calculated some pressure settings for a machine I was in charge of. The pressure gage read in 50 psi incriments, but my spreadsheet output was rounded to the nearest 1 psi. Excel will round to decimal places, not numbers, ie..1-10-100-etc..., or 0.1-0.01-0.001 etc. So I developed an equation that will round to the nearest set value, being any number, interger or otherwise. You could round to the nearest Pi if you wanted to. So here it is:
# = the number you want to round
RN = the number to round to
IP = take the interger part
Rounded Number = IP[(#/RN)+.5] * RN
Basically, take your number #, and divide it by what you want to round to, RN. Add .5 to it, and take the integer part. Then multiply by the RN number again.
Good luck to all, hope this helps someone.
Gary
# = the number you want to round
RN = the number to round to
IP = take the interger part
Rounded Number = IP[(#/RN)+.5] * RN
Basically, take your number #, and divide it by what you want to round to, RN. Add .5 to it, and take the integer part. Then multiply by the RN number again.
Good luck to all, hope this helps someone.
Gary
RE: Round to ANY value equation,, ie..25-50-100-13 anything
Number is the value to round.
Multiple is the multiple to which you want to round number.
Remarks
Box,
doesn't Excel "MROUND()" function do the same?
Here is how Excel Help explains it:
___________________________________
MROUND rounds up, away from zero, if the remainder of dividing number by multiple is greater than or equal to half the value of multiple.
Examples
MROUND(10, 3) equals 9
MROUND(-10, -3) equals -9
MROUND(1.3, 0.2) equals 1.4
MROUND(5, -2) equals #NUM!
____________________________________________
Or I did not catch something?
gearguru
RE: Round to ANY value equation,, ie..25-50-100-13 anything
Thanks for pointing this out. But.... My equation could be used anywhere. Fortran doesn't have a rounding function, or it didn't when I used to use it. And I'm sure several of the newer languages can't do that. So maybe someone can use it that way.
But thanks for the update on Excel. Just shows my own ignorance.
Gary
RE: Round to ANY value equation,, ie..25-50-100-13 anything
An application like Excel has so much functionality it's almost impossible to know everything about it. That's what is great about these forums (try this experiment: ask your question here and also ask the 'Office Assistant' then compare results
Btw, I recall using a variation on your function in a GW-BASIC program years ago!