round()

Returns a number rounded to the specified number of decimal digits.

Syntax

round(NUM_EXPR[, NUM_DIGITS])

Parameters

NUM_EXPR
The number to round. Supports 32-bit integer, 64-bit integer, 16-bit integer, 32-bit float, and 64-bit double types.
NUM_DIGITS
The number of digits to round to. Defaults to 0 if omitted. A positive value specifies decimal places; a negative value specifies places above the decimal point.

Description

The round() function rounds NUM_EXPR to the number of digits specified by NUM_DIGITS.

The return type depends on the input type:

  • For integer types (32-bit integer, 64-bit integer, 16-bit integer) with NUM_DIGITS of 0 or greater, the input value is returned as-is.
  • For integer types with a negative NUM_DIGITS, a 64-bit integer is returned.
  • For floating-point types (32-bit float, 64-bit double), a 64-bit double is returned.

Returns null in the following cases:

  • NUM_EXPR is null.
  • NUM_EXPR is an unsupported type.
  • NUM_DIGITS is specified but is not an integer type.

Error codes

N/A

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Round byte count to kilobytes.

    table limit=5 WEB_APACHE_SAMPLE | eval result = round(bytes / 1024.0)
    | fields bytes, result
    
  2. Round to one decimal place.

    table limit=5 WEB_APACHE_SAMPLE | eval result = round(bytes / 1024.0, 1)
    | fields bytes, result
    
  3. Round to the nearest hundred using a negative digit count.

    table limit=5 WEB_APACHE_SAMPLE | eval result = round(bytes, -2)
    | fields bytes, result
    
  4. NULL input

    json "{}" | eval r = round(null)
    | # r: null
    

Compatibility

The round() function is available since before Sonar 4.0.