* tests/mathtest.prg
* tests/testcgi.prg
* tests/speedold.prg
* tests/testhtml.prg
* tests/wvtext.prg
* tests/testpre.prg
* tests/testfor.prg
* doc/en-EN/string.txt
* doc/en-EN/memvar2.txt
* source/rtl/gtdos/gtdos.c
* source/rtl/gtsln/keytrans.c
* source/rtl/gtsln/mousesln.c
* source/rtl/gtsln/gtsln.c
* source/rtl/gtsln/kbsln.c
* source/rdd/dbfntx/dbfntx1.c
* source/rdd/dbfnsx/dbfnsx1.c
* source/compiler/genc.c
* contrib/gtalleg/ssf.c
* contrib/hbct/strdiff.c
* contrib/hbct/tests/csetarge.prg
* contrib/hbct/dattime2.c
* contrib/xhb/fparse.c
* contrib/xhb/ttable.prg
* contrib/xhb/freadlin.c
* contrib/xhb/cstruct.prg
* contrib/hbgt/strasint.c
* contrib/hbfbird/tests/testapi.c
* contrib/hbxbp/xbpsle.prg
* contrib/hbxbp/xbpmenubar.prg
* contrib/hbxbp/xbpfiledialog.prg
* contrib/hbxbp/xbpwindow.prg
* contrib/hbxbp/xbpdialog.prg
* contrib/hbxbp/xbp3state.prg
* contrib/hbnf/scancode.prg
* contrib/hbnf/vidmode.prg
* contrib/hbnf/vidcur.prg
* contrib/hbnf/proper.c
* contrib/hbnf/rand1.prg
* contrib/hbnf/fttext.c
* contrib/hbnf/settime.prg
* contrib/hbnf/page.prg
* contrib/hbnf/findith.prg
* contrib/hbnf/at2.prg
* contrib/hbnf/dispc.c
* contrib/hbnf/gcd.prg
* contrib/hbnf/dosver.prg
* contrib/hbnf/nooccur.prg
* contrib/hbnf/asum.prg
* contrib/hbnf/sleep.prg
* contrib/hbnf/wda.prg
* contrib/hbnf/any2any.prg
* contrib/hbnf/setdate.prg
* contrib/hbnf/amedian.prg
* contrib/hbnf/blink.prg
* contrib/hbnf/aredit.prg
* contrib/hbnf/xbox.prg
* contrib/hbnf/ftround.prg
* contrib/hbnf/hex2dec.prg
* contrib/hbnf/nwuid.prg
* contrib/hbnf/aemaxlen.prg
* contrib/hbnf/nwlstat.prg
* contrib/hbnf/invclr.prg
* contrib/hbnf/diskfunc.prg
* contrib/hbnf/scregion.prg
* contrib/hbnf/d2e.prg
* contrib/hbnf/anomatch.prg
* contrib/hbnf/cntryset.prg
* contrib/hbnf/ntow.prg
* contrib/hbnf/aeminlen.prg
* contrib/hbnf/savesets.prg
* contrib/hbnf/tbwhile.prg
* contrib/hbnf/calendar.prg
* contrib/hbnf/e2d.prg
* contrib/hbnf/sysmem.prg
* contrib/hbnf/aading.prg
* contrib/hbnf/pending.prg
* contrib/gtqtc/tests/demoqtc.prg
* contrib/gtqtc/gtqtc.cpp
* contrib/gtwvg/wvgscrlb.prg
* contrib/gtwvg/wvgclass.prg
* contrib/gtwvg/wvgax.prg
* contrib/gtwvg/wvgpaint.prg
* contrib/gtwvg/wvgdarea.prg
* contrib/gtwvg/wvgmle.prg
* contrib/gtwvg/wvglistb.prg
* contrib/gtwvg/wvggui.c
* contrib/gtwvg/wvgpushb.prg
* contrib/gtwvg/wvgsle.prg
* contrib/gtwvg/tests/demowvg.prg
* contrib/gtwvg/wincallb.c
* contrib/gtwvg/wvgmenub.prg
* contrib/gtwvg/wvgphdlr.prg
* contrib/gtwvg/wvgcheck.prg
* contrib/gtwvg/wvgstatb.prg
* contrib/gtwvg/wvgstatc.prg
* contrib/gtwvg/wvgcore.c
* contrib/gtwvg/wvgtabpg.prg
* contrib/gtwvg/wvg3stat.prg
* contrib/gtwvg/wvgsink.c
* contrib/gtwvg/wvgwing.c
* contrib/hbmisc/hb_f.c
* contrib/hbmisc/nconvert.prg
* contrib/hbmisc/doc/en/ht_file.txt
* contrib/hbvpdf/hbvpdf.prg
* contrib/hbvpdf/hbvpdft.prg
* examples/hbdoc/examples/core_es/memvar2.txt
* examples/hbmake/hbmake.prg
* examples/hbsqlit2/tests/hbsqlite.prg
* examples/guestbk/testcgi.prg
* examples/guestbk/guestbk.prg
* examples/pe/editorhi.prg
* examples/terminal/trm_cli.prg
* examples/terminal/terminal.prg
* examples/hscript/dir.hs
* examples/hscript/ugly.hs
* Minor formatting.
190 lines
6.7 KiB
Plaintext
190 lines
6.7 KiB
Plaintext
/*
|
|
* $Id$
|
|
*/
|
|
|
|
/*
|
|
* File......: round.prg
|
|
* Author....: David Husnian
|
|
* CIS ID....: ?
|
|
*
|
|
* This is an original work by David Husnian and is placed in the
|
|
* public domain.
|
|
*
|
|
* Modification history:
|
|
* ---------------------
|
|
*
|
|
* Rev 1.2 15 Aug 1991 23:05:30 GLENN
|
|
* Forest Belt proofread/edited/cleaned up doc
|
|
*
|
|
* Rev 1.1 14 Jun 1991 19:52:48 GLENN
|
|
* Minor edit to file header
|
|
*
|
|
* Rev 1.0 01 Apr 1991 01:02:08 GLENN
|
|
* Nanforum Toolkit
|
|
*
|
|
*/
|
|
|
|
|
|
/* $DOC$
|
|
* $FUNCNAME$
|
|
* FT_ROUND()
|
|
* $CATEGORY$
|
|
* Math
|
|
* $ONELINER$
|
|
* Rounds a number to a specific place
|
|
* $SYNTAX$
|
|
* FT_ROUND( <nNumber> [, <nRoundToAmount> ;
|
|
* [, <cRoundType> [, <cRoundDirection> ;
|
|
* [, <nAcceptableError> ] ] ] ] ) -> nNumber
|
|
* $ARGUMENTS$
|
|
* <nNumber> is the number to round
|
|
*
|
|
* <nRoundToAmount> is the fraction to round to or the number of places,
|
|
* default is 2.
|
|
*
|
|
* <cRoundType> is the type of rounding desired
|
|
*
|
|
* "D" for Decimal (3 for thousandth, 1/1000) (default)
|
|
* "F" for Fraction (3 for thirds, 1/3)
|
|
* "W" for Whole numbers (3 for thousand, 1000)
|
|
*
|
|
* <cRoundDirection> is the direction to round the number toward
|
|
*
|
|
* "U" to round Up 1.31 -> 1.4
|
|
* -1.31 -> -1.4
|
|
* "D" to round Down 1.36 -> 1.3
|
|
* -1.36 -> -1.3
|
|
* "N" to round Normal 1.5 -> 2
|
|
* -1.5 -> -2
|
|
* 1.49 -> 1
|
|
* -1.49 -> -1
|
|
*
|
|
* <nAcceptableError> is the amount that is considered acceptable
|
|
* to be within, i.e., if you're within this amount of the number
|
|
* you don't need to round
|
|
* $RETURNS$
|
|
* The number, rounded as specified.
|
|
* $DESCRIPTION$
|
|
* This function will allow you to round a number. The following can
|
|
* be specified:
|
|
* a. Direction (up, down or normal - normal is 4/5 convention)
|
|
* b. Type (whole, decimal, fraction)
|
|
* c. Amount (100's, 5 decimals, 16th, etc.)
|
|
* $EXAMPLES$
|
|
* // round normal to 2 decimal places
|
|
* nDollars := FT_ROUND(nDollars)
|
|
*
|
|
* // round normal to 6 decimal places
|
|
* nIntRate := FT_ROUND(nIntRate, 6)
|
|
*
|
|
* // round to nearest thousands
|
|
* nPrice := FT_ROUND(nPrice, 3, NEAREST_WHOLE_NUMBER)
|
|
*
|
|
* // round Up to nearest third
|
|
* nAmount := FT_ROUND(nAmount, 3, NEAREST_FRACTION, ROUND_UP)
|
|
*
|
|
* // round down to 3 decimals Within .005
|
|
* nAvg := FT_ROUND(nAvg, 3, , ROUND_DOWN, .005)
|
|
* $END$
|
|
*/
|
|
|
|
|
|
#define IS_NEGATIVE(x) ((x) < 0)
|
|
|
|
#define NEAREST_DECIMAL "D"
|
|
#define NEAREST_FRACTION "F"
|
|
#define NEAREST_WHOLE_NUMBER "W"
|
|
#define ROUND_DOWN "D"
|
|
#define ROUND_NORMAL "N"
|
|
#define ROUND_UP "U"
|
|
|
|
#command DEFAULT <Param1> TO <Def1> [, <ParamN> TO <DefN> ] ;
|
|
=> ;
|
|
<Param1> := iif(<Param1> == NIL,<Def1>,<Param1>) ;
|
|
[; <ParamN> := iif(<ParamN> == NIL,<DefN>,<ParamN>)]
|
|
|
|
#command DEFAULT <Param1> TO <Def1> IF NOT <Type1> ;
|
|
[, <ParamN> TO <DefN> IF NOT <TypeN> ] ;
|
|
=> ;
|
|
<Param1> := iif(VALTYPE(<Param1>) == <Type1>,<Param1>,<Def1>) ;
|
|
[; <ParamN> := iif(VALTYPE(<ParamN>) == <TypeN>,<ParamN>,<DefN>)]
|
|
|
|
|
|
|
|
FUNCTION FT_ROUND(nNumber, nRoundToAmount, cRoundType, cRoundDirection, ;
|
|
nAcceptableError)
|
|
|
|
LOCAL nResult := ABS(nNumber) // The Result of the Rounding
|
|
|
|
DEFAULT nRoundToAmount TO 2, ;
|
|
cRoundType TO NEAREST_DECIMAL, ;
|
|
cRoundDirection TO ROUND_NORMAL, ;
|
|
nAcceptableError TO 1 / (nRoundToAmount ** 2)
|
|
|
|
// Are We Rounding to the Nearest Whole
|
|
// Number or to Zero Decimal Places??
|
|
IF (LEFT(cRoundType,1) != NEAREST_WHOLE_NUMBER .AND. ;
|
|
(nRoundToAmount := INT(nRoundToAmount)) != 0)
|
|
|
|
// No, Are We Rounding to the Nearest
|
|
// Decimal Place??
|
|
IF (LEFT(cRoundType,1) == NEAREST_DECIMAL)
|
|
|
|
// Yes, Convert to Nearest Fraction
|
|
nRoundToAmount := 10 ** nRoundToAmount
|
|
|
|
ENDIF // LEFT(cRoundType,1) == NEAREST_DECIMAL
|
|
|
|
// Are We Already Within the Acceptable
|
|
// Error Factor??
|
|
IF (ABS(INT(nResult * nRoundToAmount) - (nResult * nRoundToAmount)) > ;
|
|
nAcceptableError)
|
|
// No, Are We Rounding Down??
|
|
nResult -= IIF(LEFT(cRoundDirection,1) == ROUND_DOWN, ;
|
|
; // Yes, Make Downward Adjustment
|
|
1 / nRoundToAmount / 2, ;
|
|
; // Are We Rounding Up??
|
|
IIF(LEFT(cRoundDirection,1) == ROUND_UP , ;
|
|
; // Yes, Make Upward Adjustment
|
|
-1 / (nRoundToAmount) / 2, ;
|
|
; // No, Rounding Normal, No Adjustment
|
|
0))
|
|
//Do the Actual Rounding
|
|
nResult := INT((nRoundToAmount * nResult) + .5 + nAcceptableError) / ;
|
|
nRoundToAmount
|
|
|
|
ENDIF // ABS(INT(nResult * nRoundToAmount) -
|
|
// (mResult * nRoundAmount)) >
|
|
// nAcceptableError
|
|
|
|
ELSE // Yes, Round to Nearest Whole Number
|
|
// or to Zero Places
|
|
|
|
nRoundToAmount := MAX(nRoundToAmount, 1)
|
|
|
|
DO CASE // Do "Whole" Rounding
|
|
|
|
CASE LEFT(cRoundDirection,1) == ROUND_UP
|
|
|
|
nResult := (INT(nResult / nRoundToAmount) * nRoundToAmount) + ;
|
|
nRoundToAmount
|
|
|
|
CASE LEFT(cRoundDirection,1) = ROUND_DOWN
|
|
|
|
nResult := INT(nResult / nRoundToAmount) * nRoundToAmount
|
|
|
|
OTHERWISE // Round Normally
|
|
|
|
nResult := INT((nResult + nRoundToAmount / 2) / nRoundToAmount) * ;
|
|
nRoundToAmount
|
|
|
|
ENDCASE
|
|
|
|
ENDIF // LEFT(cRoundType,1)!=NEAREST_WHOLE or
|
|
// nRoundToAmount == 0
|
|
IF IS_NEGATIVE(nNumber) // Was the Number Negative??
|
|
nResult := -nResult // Yes, Make the Result Negative Also
|
|
ENDIF // IS_NEGATIVE(nNumber)
|
|
|
|
RETURN nResult // FT_Round
|