round
Description
Rounds up a number to the nearest whole number and returns the smallest integer greater than or equal to the passed number.
Input / Parameter
value
The number to round.
Number
-
-
Yes
mode
The mode of rounding.
String
HALF_UP
CEILING, DOWN, FLOOR, HALF_DOWN, HALF_EVEN, HALF_UP, UP
Yes
mode
parameter options:
mode
parameter options:CEILING: Rounds towards positive infinity. For positive numbers, it behaves like rounding up, meaning it moves to the nearest higher integer if there's a fractional part. For negative numbers, it effectively rounds towards zero by moving up to the next higher integer (or less negative number).
DOWN: Rounds towards zero. It drops the fractional part of the number, regardless of whether the number is positive or negative. This is also known as truncation.
FLOOR: Rounds towards negative infinity. For positive numbers, it moves down to the nearest lower integer. For negative numbers, it moves away from zero to the next more negative integer.
HALF_DOWN: Rounds towards the nearest neighbor, but if the number is exactly halfway between two integers, it rounds down towards the lower integer. This applies to both positive and negative numbers.
HALF_EVEN: Rounds towards the nearest neighbor, but if the number is exactly halfway between two integers, it rounds towards the nearest even integer.
HALF_UP: Rounds towards the nearest neighbor, but if the number is exactly halfway between two integers, it rounds up towards the higher integer.
UP: Rounds away from zero. It increases the magnitude of the number to the next higher integer, regardless of whether the number is positive or negative.
Output
Returns the rounded number.
Number
Example
In this example, we will round the value 1.5
and print the result in the console.
Steps
Drag a
button
component into the canvas and open theAction
tab. Select thepress
event of the button and drag theLog.write
function to the event flow.Call the function
Math.round
inside theLog.write
function.Enter the values for the value to be rounded and the mode to be used.
Result
The console will print
2
.
Last updated