Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Round a number
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00174482
Message ID:
00174489
Views:
26
>How can I take a number like 1.345 and round it to a specific precision like .2?
>
>1.3456 precision .2 = 1.4
>1.6345 precision .5 = 1.5
>1.3456 precision .005 = 1.345
>
>I think that I need to figure out how many decmil places and ROUND() the number then somehow round to the precision
>
>
>Thanks


John, I just cooked this up and it should handle all your needs... 3 simple parameters...
? 1.3456, PreciseRound( 1.3456, .2, 1 )
? 1.6345, PreciseRound( 1.6345, .5, 1 )
? 1.3456, PreciseRound( 1.3456, .005, 3 )

*/ function to round precision
*/ Ex: PreciseRound( 123.3321, .4, 1 )
function PreciseRound( lnValue, lnPrecVal, lnDec )
   local lnNewVal, lnRem
	
   */ Put into whole number and do primary rounding
   */ before determining precision up/down...
   lnNewVal = round( lnValue, lnDec +1 )
	
   */ Now, based on precision what remainder would 
   */ be left in case not exactly divisible
   lnRem    = mod( lnNewVal, lnPrecVal )
	
   */ if over the halfway precision mark, round up
   if lnRem / lnPrecVal >= .5
      lnNewVal = lnNewVal + lnPrecVal - lnRem
   else
      lnNewVal = lnNewVal - lnRem
   endif
return( round( lnNewVal, lnDec ) )
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform