Date: 02-16-2022
Return to Index
created by gbSnippets
'Rounding is not so simple as you might think. In the old days, 0.5 was
'rounded up, all else was rounded down. PowerBASIC options mostly use
'Banker's rounding - x.5 rounds to nearest EVEN integer.
'Primary Code:
x% = Round(0.2,0) 'returns 0
x% = Round(0.5,0) 'returns 0 (nearest even integer - Banker's rounding)
x! = Round(5.247,2) 'returns 5.25
'See the compilable example further down this page.
'There are actually 8 function which provide rounding of floating point numbers
'each varies on how numbers are rounded, or whether a number/string is returned
Round '- round to specified number of decimal places
Int, Ceil, Cint '-round to integer values
Format$, Using$ '-mask$ used to determine format of returned string
Fix, Frac '-truncate/return fractional part
'Example #1 - Round
'x = ROUND(numeric_expression, n)
'Banker's rounding - if franctional part is exactly 5, rounds to nears even integer
'this method gives better averaging of results over rounding of many numbers
'n is number of decimal places required in the result.
'this method DOES NOT "round up at five"
x% = Round(0.5, 0) ' returns 0
x% = Round(1.5, 0) ' returns 2
x% = Round(2.5, 0) ' returns 2
x% = Round(2.51, 0) ' returns 3
'Example #2 - INT
'x = INT(numeric_expression)
'rounds down to nearest integer
x = Int -1.1 'returns -2
x = Int -0.5 'returns -1
x = Int -0.1 'returns -1
x = Int 0.4 'returns 0
x = Int 0.9 'returns 0
x = Int 1.4 'returns 1
x = Int 1.9 'returns 1
'Example #3 - CEIL
'y = CEIL(numeric_expression)
'rounds up to nearest integer
x = Int -1.1 'returns -1
x = Int -0.5 'returns 0
x = Int -0.1 'returns 0
x = Int 0.4 'returns 1
x = Int 0.9 'returns 1
x = Int 1.4 'returns 2
x = Int 1.9 'returns 2
'Example #4 - CINT
'x = CINT(numeric_expression)
'same rounding as ROUND function, but rounds to integer values only
'uses Banker's rounding - if fractional part is exactly 5, rounds to nearest
'even integer. This method DOES NOT "round up at five".
x% = Round(0.5) 'returns 0
x% = Round(1.5) 'returns 2
x% = Round(2.5) 'returns 2
x% = Round(2.5) 'returns 3
'Example #5 - FORMAT$
'x$ = FORMAT$(num_expression [, [digits& | fmt$]])
'returns a string representation of a number
'uses Banker's rounding - if franctional part is exactly 5, rounds to nears even integer
'fmt$ defines how the number is displayed - has up to 3 parts, for numbers <0, 0, or >0
fmt$ = "00.0" 'all numbers use the same display format
fmt$ = "-0.0, 0, +0.0" 'displays negative/positive signs for minus/plus numbers
A$ = Format$(0.5##, "0") 'returns 0
A$ = Format$(1.5##, "0") 'returns 2
A$ = Format$(2.5##, "0") 'returns 2
A$ = Format$(2.51##, "0") 'returns 3
'Example #6 - USING$
'sResult$ = USING$(fmtmask$, expr [, expr [, ...]])
'can be applied to both string or numeric inputs
A$ = Using$("##", 0.5) 'returns 1
A$ = Using$("##", 1.5) 'returns 2
A$ = Using$("##", 2.5) 'returns 2
A$ = Using$("##", 2.51) 'returns 3
'===== The next two options truncate, not round! ============
'Jump for completeness, here are two other functions
'that fit in the family of rounding functions.
'Example #7 - FIX
'x = FIX(numeric_expression)
'truncates the fractional part, which is neither round up nor round down
x% = Fix(5.2) 'returns 5
x% = Fix(1.8) 'returns 1
x% = Fix(-2.3) 'returns -2
'Example #8 - FRAC
'x = FRAC(numeric expression)
'returns fractional part of a floating point number
x = Frac(5.12) 'returns 0.12
x = Frac(4.8) 'returns 0.8
x = Frac(-7.4) 'returns -0.4
'gbs_00199
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm