Help with rounding
Author Message
DMurray

 Posted: Visual FoxPro General, Help with rounding Top
 Help!  I have to round a number to the closest 500 but if last three digits are between 0 - 250 round down, 251 - 499 round up, 500 - 750 round down, 751 up, round up. Example: 2140 should round to 2000 2257 should round to 2500 2660 should round to 2500 2760 should round to 3000 Thanks in advance! Visual FoxPro2

giuln

 Posted: Visual FoxPro General, Help with rounding Top
 Can you test Mod(, 1000) to make round Mod(2140, 500) = 140 -> Down ( - 140) Mod(2257, 500) = 257 -> Up (simply - 257 + 500) Mod(2660, 500) = 160 -> Down ( - 160) Mod(2760, 500) = 260 -> Up (2760 - 260 + 500) and so on...

Tamar E. Granor

 Posted: Visual FoxPro General, Help with rounding Top
 Expanding on Giulio's idea, here's a function for you. I've only tested this with your data values, so be sure to test more extensively. FUNCTION RoundToNearest(nNum, nNearest) nRemainder = MOD(nNum, nNearest) IF nRemainder > nNearest/2 nResult = INT(nNum/nNearest) * nNearest + nNearest ELSE nResult = INT(nNum/nNearest) * nNearest ENDIF RETURN nResult Call it like this: nRounded = RoundToNearest(nValue, 500) Tamar

DMurray

 Posted: Visual FoxPro General, Help with rounding Top
 Thank you everyone for your help!

giuln

 Posted: Visual FoxPro General, Help with rounding Top
 You could use a third parameter to make math round, round plus or round minus to nearest, with math round as default... ;-)