How to round off to nearest 0.25

  • Thread starter Thread starter Darrell Childress
  • Start date Start date
D

Darrell Childress

I'm trying to round a number to the nearest 1/4. My best stab at it
would be to take the Integer of a number Int(Mynumber) and then take the
decimal part of the number (I don't know how to do that) and then use
nested If statements to find the nearest 0.25. Then, I would add the
results of those 2 calculations. My question is, is there a better more
direct way to do this and what is the syntax for getting the decimal
part of a number?
Thanks,
Darrell
 
This expression should get you pretty close:

(((MyNumber * 100) + 12.1) \ 25) / 100 * 25
 
Darrell said:
I'm trying to round a number to the nearest 1/4. My best stab at it
would be to take the Integer of a number Int(Mynumber) and then take the
decimal part of the number (I don't know how to do that) and then use
nested If statements to find the nearest 0.25. Then, I would add the
results of those 2 calculations. My question is, is there a better more
direct way to do this and what is the syntax for getting the decimal
part of a number?


This might ne what you want:
Int(Num * 4 + .5) / 4

You can get the fractional part of a number with:
Num - Int(Num)
 
I'm trying to round a number to the nearest 1/4. My best stab at it
would be to take the Integer of a number Int(Mynumber) and then take the
decimal part of the number (I don't know how to do that) and then use
nested If statements to find the nearest 0.25. Then, I would add the
results of those 2 calculations. My question is, is there a better more
direct way to do this and what is the syntax for getting the decimal
part of a number?
Thanks,
Darrell

Yes, there is: multiply by 4 to get each quarter to an integer,
truncate that, and divide again:

Fix([field] * 4) / 4.

If you should ever need it, you can get the fractional part of a
number using

[field] - Fix([field])


John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
Thank you very much for the help, guys. I noticed that one method always
rounds down to the nearest 1/4 (e.g., 9.73 would give a result of 9.5)
and the other method rounds to the true nearest 1/4 (e.g., 9.73 would
give a result of 9.75). I need to do a little research on what I need,
which will then dictate which method to use. Either way, I have the
answer I need.
Thanks again!
Darrell

John said:
I'm trying to round a number to the nearest 1/4. My best stab at it
would be to take the Integer of a number Int(Mynumber) and then take the
decimal part of the number (I don't know how to do that) and then use
nested If statements to find the nearest 0.25. Then, I would add the
results of those 2 calculations. My question is, is there a better more
direct way to do this and what is the syntax for getting the decimal
part of a number?
Thanks,
Darrell


Yes, there is: multiply by 4 to get each quarter to an integer,
truncate that, and divide again:

Fix([field] * 4) / 4.

If you should ever need it, you can get the fractional part of a
number using

[field] - Fix([field])


John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
Thank you very much for the help, guys. I noticed that one method always
rounds down to the nearest 1/4 (e.g., 9.73 would give a result of 9.5)
and the other method rounds to the true nearest 1/4 (e.g., 9.73 would
give a result of 9.75). I need to do a little research on what I need,
which will then dictate which method to use. Either way, I have the
answer I need.

Sorry I was hasty there - yes, my suggestion indeed rounds down.
Adding .125 as suggested elsethread will round to the nearest.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
Back
Top