_ROUND

From QB64 Phoenix Edition Wiki
Revision as of 23:18, 7 May 2025 by RhoSigma (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The _ROUND function rounds SINGLE, DOUBLE and _FLOAT decimal point numbers to the nearest INTEGER, LONG or _INTEGER64 value with one exception. If the fraction of the decimal point number is exactly .5, then it is always rounded to the nearest even integer value.


Syntax

value = _ROUND(expression)


Parameters

  • expression is any type of literal or variable numerical value or mathematical calculation.


Description

Note
  • As given values may be positive or negative and the rounding directions are opposite in each case, we don't speak of rounding up or down. That's elementary school terminology when kids don't know of negative numbers yet.
  • Instead we speak of rounding towards zero or rounding towards infinity, which exactly describes the rounding direction for both, positive and negative numbers.
  • Fractions greater than .5 are rounded to the next integer number towards infinity
  • Fractions lower than .5 are rounded to the next integer number towards zero.
  • Fractions exactly .5 are rounded to the nearest even integer number.
  • This function practically superseds CINT and CLNG, as it can handle all floating point types and returns appropriate integer types, while the mentioned legacy commands imposed limits on their inputs and results.
  • This function is automatically applied (in the background) to decimal point values, which are given to SUBs or FUNCTIONs requiring _INTEGER64s as arguments.


Availability


Examples

Example
Shows how _ROUND rounds values depending on the current fraction.
WIDTH 50, 35
FOR f = -3.5 TO 3.5
    IF f < 0 THEN
        down$ = "towards infinity"
        up$ = "towards zero"
    ELSE
        down$ = "towards zero"
        up$ = "towards infinity"
    END IF
    PRINT f - 0.01, _ROUND(f - 0.01), down$
    PRINT f, _ROUND(f), "to nearest even"
    PRINT f + 0.01, _ROUND(f + 0.01), up$
    PRINT
NEXT f


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link