round, round up / down

Prev Next

Function Names

round, round up, round down

Description

Rounds the number (automatically or up or down) to the next rounding interval. The rounding intervals may be integers as well as non-integers. Example: 0.25 rounds in steps of 0.25.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1, 2

Parameters

No.TypeDescription
1
input
numeral Number to round

Any positive or negative number

Opt. 2
input
numeral Rounding interval

Defines the steps how the rounding is done. E.g. 0.4 rounds numbers to 0, 0.4, 0.8, 1.2, and the same in the negative direction.

Return value

TypeDescription
numeral Result

Rounded number

Exceptions

Values provided is not a numerals
rounding intervals is negative or zero

Examples

      for all parameters( { {1.4, 1}, {1.5,1}, {-1.5,1}, {18, 10}, { 3.14159, 0.01 } }, p[] )
          echo( "round ", p[]{0}, " using ", p[]{1}, ": ", round( p[]{0}, p[]{1} ),
                "  up: ", round up( p[]{0}, p[]{1} ), "  down: ", round down( p[]{0}, p[]{1} ) );

Output

round 1.4 using 1: 1  up: 2  down: 1
round 1.5 using 1: 2  up: 2  down: 1
round -1.5 using 1: -2  up: -2  down: -1
round 18 using 10: 20  up: 20  down: 10
round 3.14159 using 0.01: 3.14  up: 3.15  down: 3.14
Try it yourself: Open LIB_Function_round.b4p in B4P_Examples.zip. Decompress before use.