latest

Operators

Round To Decimal Place

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

round
decimal
place
truncate
nearest
floor
ceiling

Operators

Trigonometric

Tools

Constants

Round To Decimal Place

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.

Problem Addressed

Numerical results often have more decimal places than necessary or desired. This node helps by:

  • Standardizing Precision: Ensuring numbers conform to a specific number of decimal places (e.g., currency to 2 decimal places).
  • Simplifying Display: Making numbers easier to read and understand by removing excessive trailing digits.
  • Integer Conversion: Rounding floating-point numbers to integers using various rounding methods.
  • Rounding to Tens/Hundreds: Adjusting numbers to the nearest 10, 100, or other powers of 10.

How It Works

The node takes a Number X, a Decimal Place (integer), and a Round Type:

  • Number X: The input number you want to round.
  • Decimal Place: An integer that specifies the number of digits to keep after the decimal point.
    • Positive integer (e.g., 2): Rounds to that many decimal places (e.g., hundredths).
    • Zero (0): Rounds to the nearest integer.
    • Negative integer (e.g., -1): Rounds to the corresponding power of 10 (e.g., -1 rounds to tens, -2 to hundreds).
  • Round Type:
    • 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: The result of the rounding operation.
  • Difference: The value of Rounded - Number X.

Examples

  1. Rounding currency to 2 decimal places (Nearest):

    • Number X: 12.345
    • Decimal Place: 2
    • Round Type: 0 (Nearest)
    • Rounded: 12.35 (Note: Math.round(12.345 * 100) / 100 = Math.round(1234.5)/100 = 1235/100 = 12.35)
    • Difference: 0.005
  2. Rounding a negative number to 2 decimal places (Nearest, tie-breaking):

    • Number X: -12.345
    • Decimal Place: 2
    • Round Type: 0 (Nearest)
    • Rounded: -12.34 (Note: Math.round(-12.345 * 100) / 100 = Math.round(-1234.5)/100 = -1234/100 = -12.34)
    • Difference: 0.005
  3. Truncating/Flooring a number to 1 decimal place:

    • Number X: 7.89
    • Decimal Place: 1
    • Round Type: 1 (Floor / Truncate)
    • Rounded: 7.8
    • Difference: -0.09
  4. Rounding a score up to the nearest integer:

    • Number X: 88.1
    • Decimal Place: 0
    • Round Type: 2 (Ceiling)
    • Rounded: 89
    • Difference: 0.9
  5. Rounding a population figure to the nearest hundred (d = -2):

    • Number X: 12345
    • Decimal Place: -2 (for hundreds)
    • Round Type: 0 (Nearest)
      • 12345 * Math.pow(10, -2) = 123.45. Math.round(123.45) = 123. 123 / Math.pow(10, -2) = 12300.
    • Rounded: 12300
    • Difference: -45
  6. Floor rounding a negative number to 1 decimal place:

    • Number X: -5.56
    • Decimal Place: 1
    • Round Type: 1 (Floor / Truncate)
      • -5.56 * Math.pow(10,1) = -55.6. Math.floor(-55.6) = -56. -56 / Math.pow(10,1) = -5.6.
    • Rounded: -5.6
    • Difference: -0.04 (-5.6 - (-5.56))

Inputs

NameTypeDescriptionDefault Value
x
Number
Number to round
decimalPlace
Number
Decimal place to round to0
roundType
Number
Determines how the number is rounded 0 = Nearest 1 = Floor / Truncate 2 = Ceiling0

Outputs

NameTypeDescription
rounded
Number
Resulting rounded number
difference
Number
Difference between rounded and input x