floor()

The floor() function returns a number rounded down to the specified number of decimal digits.

Syntax

floor(NUM_EXPR[, NUM_DIGITS])

Parameters

NUM_EXPR
A numeric expression to round down. Supports 32-bit integer, 64-bit integer, 16-bit integer, 32-bit float, and 64-bit double types.
NUM_DIGITS
(Optional) The number of decimal digits to round down to. A positive value rounds down at the specified decimal place. Zero rounds down at the first decimal place. A negative value rounds down at the corresponding place above the decimal point. Defaults to 0 if omitted.

Description

The floor() function rounds down the value of NUM_EXPR at the position specified by NUM_DIGITS.

For integer types (32-bit integer, 64-bit integer, 16-bit integer), if NUM_DIGITS is 0 or greater, no rounding is needed and the input value is returned as-is. If NUM_DIGITS is negative, rounding is applied at the specified position and a 64-bit integer is returned.

For floating-point types (32-bit float, 64-bit double), the floor calculation is always applied.

Returns null if NUM_EXPR is null or not a number. Returns null if NUM_DIGITS is specified but is not an integer type (32-bit integer, 64-bit integer, or 16-bit integer).

Error codes

N/A

Usage examples

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

  1. Round down byte count to kilobytes.

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

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

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

    json "{'val': null}" | eval result = floor(val)
    | # result: null
    

Compatibility

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