BigQuery · Math function
BigQuery ROUND
ROUND(x, n) rounds x to n decimal places, with halfway cases going away from zero: ROUND(19.987, 2) returns 19.99. Without n it rounds to a whole number. INT64 and FLOAT64 inputs return FLOAT64; use NUMERIC for exact money values and 'ROUND_HALF_EVEN' for banker's rounding.
ROUND rounds a number to the nearest integer, or to N digits after the decimal point. A negative N rounds to the left of the decimal point: ROUND(1234.5, -2) is 1200.0. Halfway cases round away from zero, so ROUND(2.5) is 3.0 and ROUND(-2.5) is -3.0. Python and pandas round halves to the nearest even digit by default, so results can differ when you compare the two.
The return type follows the input: INT64 and FLOAT64 return FLOAT64 (ROUND(5) is 5.0), NUMERIC returns NUMERIC and BIGNUMERIC returns BIGNUMERIC. FLOAT64 can't store most decimal fractions exactly: the literal 1.005 is really 1.00499999..., so ROUND(1.005, 2) returns 1.0. Keep money in NUMERIC, where ROUND(NUMERIC '1.005', 2) is 1.01.
For NUMERIC and BIGNUMERIC values you can pass a third argument, rounding_mode: 'ROUND_HALF_AWAY_FROM_ZERO' (the default) or 'ROUND_HALF_EVEN'. Passing a mode with a FLOAT64 value fails with No matching signature for function ROUND. ROUND changes the value, not its display; to always show two decimals, format it as text with FORMAT: FORMAT('%.2f', 2.5) returns '2.50'.
Syntax
ROUND(X [, N [, rounding_mode]])Arguments and return value
| Argument | Description |
|---|---|
X | The number to round: INT64, NUMERIC, BIGNUMERIC or FLOAT64. |
N | Optional INT64. Digits to keep after the decimal point; negative values round to tens, hundreds and so on. Defaults to 0. |
rounding_mode | Optional, NUMERIC and BIGNUMERIC only. 'ROUND_HALF_AWAY_FROM_ZERO' (default) or 'ROUND_HALF_EVEN'. Other strings fail, and FLOAT64 input doesn't accept a mode. |
Returns: FLOAT64 for INT64 or FLOAT64 input, NUMERIC for NUMERIC and BIGNUMERIC for BIGNUMERIC. Raises an error if the rounded value overflows the type.
Examples
Decimals, whole numbers and negative N
SELECT
ROUND(19.987, 2) AS two_decimals,
ROUND(19.5) AS whole_number,
ROUND(-19.5) AS negative_half,
ROUND(1234.5, -2) AS hundreds;| two_decimals | whole_number | negative_half | hundreds |
|---|---|---|---|
| 19.99 | 20.0 | -20.0 | 1200.0 |
FLOAT64 vs NUMERIC on a halfway value
The FLOAT64 literal 1.005 is stored as 1.00499999999999989..., so it rounds down. The NUMERIC value is exact and rounds up.
SELECT
ROUND(1.005, 2) AS float64_result,
ROUND(NUMERIC '1.005', 2) AS numeric_result;| float64_result | numeric_result |
|---|---|
| 1.0 | 1.01 |
Banker's rounding with ROUND_HALF_EVEN
With 'ROUND_HALF_EVEN', halves go to the nearest even digit: 2.5 becomes 2 and 3.5 becomes 4. The default mode rounds 2.5 to 3.
SELECT
ROUND(NUMERIC '2.5', 0, 'ROUND_HALF_EVEN') AS half_even_2_5,
ROUND(NUMERIC '3.5', 0, 'ROUND_HALF_EVEN') AS half_even_3_5,
ROUND(NUMERIC '2.5') AS default_2_5,
ROUND(NUMERIC '2.345', 2, 'ROUND_HALF_EVEN') AS half_even_cents;| half_even_2_5 | half_even_3_5 | default_2_5 | half_even_cents |
|---|---|---|---|
| 2 | 4 | 3 | 2.34 |
Round each line or round the total?
Rounding every invoice line before summing gives 30.03; rounding the exact total gives 30.02. Pick the rule your finance team uses and apply it the same way everywhere.
WITH invoice_lines AS (
SELECT 'INV-7' AS invoice_id, NUMERIC '10.005' AS net_amount UNION ALL
SELECT 'INV-7', NUMERIC '10.005' UNION ALL
SELECT 'INV-7', NUMERIC '10.005'
)
SELECT
invoice_id,
SUM(ROUND(net_amount, 2)) AS sum_of_rounded_lines,
ROUND(SUM(net_amount), 2) AS rounded_total
FROM invoice_lines
GROUP BY invoice_id;| invoice_id | sum_of_rounded_lines | rounded_total |
|---|---|---|
| INV-7 | 30.03 | 30.02 |
Common mistakes
- Rounding money stored as FLOAT64. Binary floats turn 1.005 into 1.00499999..., so
ROUND(1.005, 2)returns 1.0. Store amounts as NUMERIC or BIGNUMERIC. - Passing a rounding mode with FLOAT64.
ROUND(2.5, 0, 'ROUND_HALF_EVEN')fails with No matching signature for function ROUND; cast first:ROUND(CAST(x AS NUMERIC), 0, 'ROUND_HALF_EVEN'). - Inventing a mode name. Only the two documented modes exist;
'ROUND_HALF_UP'fails with Could not cast literal "ROUND_HALF_UP" to type ROUNDING_MODE. - Expecting an integer back.
ROUND(price)returns FLOAT64 such as 20.0. UseCAST(ROUND(price) AS INT64), orCAST(price AS INT64), which also rounds halves away from zero. - Expecting trailing zeros.
ROUND(2.5, 2)is the number 2.5, and numbers carry no formatting. UseFORMAT('%.2f', x)when you need the text 2.50.
Cost and performance
- NUMERIC values take 16 logical bytes and BIGNUMERIC 32, against 8 for FLOAT64. The exactness is worth it for money, but on very large tables it doubles or quadruples the bytes stored and scanned for that column.
FAQ
How do I round to 2 decimal places in BigQuery?
Use ROUND(x, 2). For money, keep x as NUMERIC so halfway values round correctly, and use FORMAT('%.2f', x) if the output must always show two decimals.
Does BigQuery ROUND use banker's rounding?
Not by default: halfway cases round away from zero. For NUMERIC and BIGNUMERIC values, pass 'ROUND_HALF_EVEN' as the third argument, for example ROUND(amount, 2, 'ROUND_HALF_EVEN').
How do I always round up or down?
Why does ROUND return 3.0 instead of 3?
ROUND returns FLOAT64 for INT64 and FLOAT64 input, and the console shows whole FLOAT64 values with .0. Use CAST(ROUND(x) AS INT64) to get an integer.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
