Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Rounding to a decimal
Message
De
10/09/2002 20:12:10
 
 
À
10/09/2002 14:50:34
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00698965
Message ID:
00699142
Vues:
18
I can't think of a VFP built-in function to do it, but this seems to work - (Not much code here, mostly comments)

For the record, this is not the code I originally posted. This is an update. I realized that the original code can be very slow if using the full number and that number is very large. This code is nearly a bjillion times faster for large numbers (I timed it):
************************************************************
* Procedure...: DecRound
* Parameters..: tnVal, tnTrimVal, tlFullVal
*		tnVal is given value
*		tnTrimVal is 'set decimal' value to round to
*		tlFullVal is a logical. .T. means use the full
*		number for rounding, otherwise, just use
*		the decimal portion
*
* Author......: Alan Popow
* Created.....: September 10, 2002
*
* Purpose.....:	Round to the nearest tnTrimVal multiple
*	ie - DecTrim(5.44, .25) will round 5.44 to
*		nearest .25 - this will return 5.50
*	decround(7.637, .222, .T.) will return 7.548,
*		because 7.767 is closer to 7.548 than to
*		7.770, but
*	decround(7.637, .222) will return 7.666
*		because .637 is closer to .666 than to
*		.444
*if anyone understands what I just said, I'd
*	appreciate an explanation.
* Called by...: Vrs
*
* Returns.....: lnRetult - Rounded Value
*
* Notes.......:
************************************************************
Function DecRound(tnVal, tnTrimVal, tlFullVal)
LOCAL lnResult, lnLow, lnHi, lnSaveInt

IF PCOUNT() < 3
   tlFullVal = .F.
ENDIF

* save and dump integer portion if not using full value
IF !tlFullVal
   lnSaveInt = INT(tnVal)
   tnVal = tnVal - lnSaveInt
ENDIF

* find value next lowest to tnVal
lnLow = tnTrimval * (INT(tnVal/tnTrimVal))

* find next higher value
lnHi = lnLow + tnTrimVal

* compare lower and higher values to tnval
lnResult = IIF((lnHi - tnVal) > (tnVal - lnLow), lnLow, lnHi)

IF !tlFullVal
   lnResult = lnResult + lnSaveInt
ENDIF

RETURN lnResult
You'll need to build in some error checking, but as far as I can tell, this works.

Alan

>Hello,
>
>Is there a way in 1 function to round to the nearest set decimal quantity? I need to use this in a report field.
>
>For example:
>
>Nearest .25
>5.44 rounds to 5.5
>
>Nearest .10
>4.67 rounds to 4.7
>
>Thanks,
>
>Jeff
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform