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.


Vectorization: These functions support vectorization in the 1st and 2nd function parameters. Instead of providing a single value, you can provide a set or even a nested set which contain multiple values. The function will then process every value and its return value contains a corresponding set containing all results.

Call as: function

Restrictions

Indirect parameter passing is disabled
Vectorization is allowed in the 1st and 2nd function parameters

Parameter count

1, 2

Parameters

No.TypeDescription
1
input
numeral
set
Number to round

Any positive or negative number. Multiple numbers can be rounded if put into a set (vectorization).

Opt. 2
input
numeral
set
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
set
Result

Rounded number(s)

Exceptions

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

Examples

               for all( { {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.