A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/sql-reference/functions/round below:

Website Navigation


ROUND | Snowflake Documentation

Categories:

Numeric functions (Rounding and Truncation)

ROUND

Returns rounded values for input_expr.

See also:

CEIL , FLOOR , TRUNCATE , TRUNC

Syntax
ROUND( <input_expr> [ , <scale_expr> [ , '<rounding_mode>' ] ] )

Copy

ROUND( EXPR => <input_expr> ,
       SCALE => <scale_expr>
       [ , ROUNDING_MODE => '<rounding_mode>'  ] )

Copy

Arguments

Required:

input_expr . OR . EXPR => input_expr

The value or expression to operate on. The data type must be one of the numeric data types, such as FLOAT or NUMBER.

If you specify the EXPR => named argument, you must also specify the SCALE => named argument.

Optional:

scale_expr . OR . SCALE => scale_expr

The number of digits the output includes after the decimal point. The expression must evaluate to an integer from -38 to +38.

The default scale_expr is zero, meaning that the function removes all digits after the decimal point.

For information about negative numbers, see Usage notes.

If you specify the SCALE => named argument, you must specify EXPR => as the preceding named argument.

'rounding_mode' . OR . ROUNDING_MODE => 'rounding_mode'

The rounding mode to use. You can specify one of the following values:

Default: HALF_AWAY_FROM_ZERO

If you specify the ROUNDING_MODE => named argument, you must specify both EXPR => and SCALE => as preceding named arguments.

Returns

The return type is based on the input type:

If the scale is zero, then the value is effectively an INTEGER.

For example:

If either the input_expr or the scale_expr is NULL, the function returns NULL.

Usage notes Examples

This following example shows a simple use of ROUND, with the default number of decimal places (0):

SELECT ROUND(135.135), ROUND(-975.975);

Copy

+----------------+-----------------+
| ROUND(135.135) | ROUND(-975.975) |
|----------------+-----------------|
|            135 |            -976 |
+----------------+-----------------+

The next example queries the data in the following table:

CREATE TABLE test_ceiling (n FLOAT, scale INTEGER);

INSERT INTO test_ceiling (n, scale) VALUES
  (-975.975, -1),
  (-975.975,  0),
  (-975.975,  2),
  ( 135.135, -2),
  ( 135.135,  0),
  ( 135.135,  1),
  ( 135.135,  3),
  ( 135.135, 50),
  ( 135.135, NULL);

Copy

Query the table and use a range of values for the scale_expr argument:

SELECT n, scale, ROUND(n, scale)
  FROM test_ceiling
  ORDER BY n, scale;

Copy

+----------+-------+-----------------+
|        N | SCALE | ROUND(N, SCALE) |
|----------+-------+-----------------|
| -975.975 |    -1 |        -980     |
| -975.975 |     0 |        -976     |
| -975.975 |     2 |        -975.98  |
|  135.135 |    -2 |         100     |
|  135.135 |     0 |         135     |
|  135.135 |     1 |         135.1   |
|  135.135 |     3 |         135.135 |
|  135.135 |    50 |         135.135 |
|  135.135 |  NULL |            NULL |
+----------+-------+-----------------+

The next two examples show the difference between using the default rounding mode ('HALF_AWAY_FROM_ZERO') and the rounding mode 'HALF_TO_EVEN'. Both examples call the ROUND function twice, first with the default rounding behavior, then with 'HALF_TO_EVEN'.

The first example uses a positive input value of 2.5:

SELECT ROUND(2.5, 0), ROUND(2.5, 0, 'HALF_TO_EVEN');

Copy

+---------------+-------------------------------+
| ROUND(2.5, 0) | ROUND(2.5, 0, 'HALF_TO_EVEN') |
|---------------+-------------------------------|
|             3 |                             2 |
+---------------+-------------------------------+

The second example uses a negative input value of -2.5:

SELECT ROUND(-2.5, 0), ROUND(-2.5, 0, 'HALF_TO_EVEN');

Copy

+----------------+--------------------------------+
| ROUND(-2.5, 0) | ROUND(-2.5, 0, 'HALF_TO_EVEN') |
|----------------+--------------------------------|
|             -3 |                             -2 |
+----------------+--------------------------------+

The next two examples demonstrate how to specify the arguments to the function by name, rather than by position:

SELECT ROUND(
  EXPR => -2.5,
  SCALE => 0) AS named_arguments;

Copy

+-----------------+
| NAMED_ARGUMENTS |
|-----------------|
|              -3 |
+-----------------+
SELECT ROUND(
  EXPR => -2.5,
  SCALE => 0,
  ROUNDING_MODE => 'HALF_TO_EVEN') AS named_with_rounding_mode;

Copy

+--------------------------+
| NAMED_WITH_ROUNDING_MODE |
|--------------------------|
|                       -2 |
+--------------------------+

The next example shows that FLOAT values aren’t always stored exactly. As you can see below, in some cases .005 is rounded to .01, while in other cases it is rounded to 0. The difference isn’t in the rounding; the difference is actually in the underlying representation of the floating point number, because 1.005 is stored as a number very slightly smaller than 1.005 (approximately 1.004999). The DECIMAL value, however is stored as an exact number, and is rounded to .01 as expected in all cases.

Create and load a table:

CREATE OR REPLACE TEMP TABLE rnd1(f float, d DECIMAL(10, 3));

INSERT INTO rnd1 (f, d) VALUES
  ( -10.005,  -10.005),
  (  -1.005,   -1.005),
  (   1.005,    1.005),
  (  10.005,   10.005);

Copy

Show examples of the difference between rounded FLOAT values and rounded DECIMAL values:

SELECT f,
       ROUND(f, 2),
       d,
       ROUND(d, 2)
  FROM rnd1
  ORDER BY 1;

Copy

+---------+-------------+---------+-------------+
|       F | ROUND(F, 2) |       D | ROUND(D, 2) |
|---------+-------------+---------+-------------|
| -10.005 |      -10.01 | -10.005 |      -10.01 |
|  -1.005 |       -1    |  -1.005 |       -1.01 |
|   1.005 |        1    |   1.005 |        1.01 |
|  10.005 |       10.01 |  10.005 |       10.01 |
+---------+-------------+---------+-------------+

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4