Round a number to a decimal place or truncate a number at a decimal place.
ID
math-operators-round-to-decimal-place
Name
Round To Decimal Place
Group
Operators
Package
Math
Keywords
This node rounds a Number X
to a specified Decimal Place
. You can control the rounding behavior using Round Type
. It's useful for standardizing numerical precision, preparing numbers for display, or ensuring values meet specific formatting requirements.
For instance, if you have a calculated value like 17.34829
but only need two decimal places for display or further calculation, this node can round it to 17.35
. It can also round to whole numbers, or even to multiples of 10, 100, etc., by using negative values for Decimal Place
.
Numerical results often have more decimal places than necessary or desired. This node helps by:
The node takes a Number X
, a Decimal Place
(integer), and a Round Type
:
2
): Rounds to that many decimal places (e.g., hundredths).0
): Rounds to the nearest integer.-1
): Rounds to the corresponding power of 10 (e.g., -1
rounds to tens, -2
to hundreds).0
(Nearest): Rounds Number X
to the value at the specified Decimal Place
that is closest. If Number X
is exactly halfway, it rounds to the number whose last digit is further from zero (standard Math.round
behavior).1
(Floor / Truncate): Rounds Number X
down. For positive Decimal Place
values, this effectively truncates the number at the specified decimal place for positive Number X
. For negative Number X
, it rounds towards negative infinity (e.g., -1.23 with d=1 becomes -1.3 if using floor, or -1.2 if strictly truncating). The node uses Math.floor
for this operation.2
(Ceiling): Rounds Number X
up to the value at the specified Decimal Place
that is greater than or equal to Number X
.The node outputs:
Rounded
- Number X
.Rounding currency to 2 decimal places (Nearest):
12.345
2
0
(Nearest)12.35
(Note: Math.round(12.345 * 100) / 100
= Math.round(1234.5)/100
= 1235/100
= 12.35
)0.005
Rounding a negative number to 2 decimal places (Nearest, tie-breaking):
-12.345
2
0
(Nearest)-12.34
(Note: Math.round(-12.345 * 100) / 100
= Math.round(-1234.5)/100
= -1234/100
= -12.34
)0.005
Truncating/Flooring a number to 1 decimal place:
7.89
1
1
(Floor / Truncate)7.8
-0.09
Rounding a score up to the nearest integer:
88.1
0
2
(Ceiling)89
0.9
Rounding a population figure to the nearest hundred (d = -2):
12345
-2
(for hundreds)0
(Nearest)
12345 * Math.pow(10, -2) = 123.45
. Math.round(123.45) = 123
. 123 / Math.pow(10, -2) = 12300
.12300
-45
Floor rounding a negative number to 1 decimal place:
-5.56
1
1
(Floor / Truncate)
-5.56 * Math.pow(10,1) = -55.6
. Math.floor(-55.6) = -56
. -56 / Math.pow(10,1) = -5.6
.-5.6
-0.04
(-5.6 - (-5.56)
)Inputs
Name | Type | Description | Default Value |
---|---|---|---|
x | Number | Number to round | |
decimalPlace | Number | Decimal place to round to | 0 |
roundType | Number | Determines how the number is rounded 0 = Nearest 1 = Floor / Truncate 2 = Ceiling | 0 |
Outputs
Name | Type | Description |
---|---|---|
rounded | Number | Resulting rounded number |
difference | Number | Difference between rounded and input x |