How to Round Numbers with MariaDB ROUND()

MariaDB’s ROUND(X, D) rounds X to D decimal places. Omit D for whole numbers or use a negative value to round to tens, hundreds, or larger units. Exact decimal ties go away from zero, so ROUND(1.5) returns 2 and ROUND(-1.5) returns -2.

Choose how many decimal places to retain

The second argument to ROUND() specifies the number of decimal places to keep. With D = 2, the result keeps two decimal places and the next digit determines whether to round away from zero.

SELECT ROUND(1.4) AS pos_below,
       ROUND(1.5) AS pos_tie,
       ROUND(-1.4) AS neg_below,
       ROUND(-1.5) AS neg_tie,
       ROUND(1.45, 1) AS one_place,
       ROUND(1.456, 2) AS two_places;

In column order, the results are 1, 2, -1, -2, 1.5, and 1.46. These decimal literals use no exponent and are treated as exact values in the calculation.

Round to tens or hundreds

With D = -1, MariaDB rounds to the nearest multiple of ten. With D = -2, it rounds to the nearest multiple of one hundred. The table changes only D while keeping the input at 1256.789.

D Rounding precision Result
2 Two decimal places 1256.79
0 Whole number 1257
-2 Nearest hundred 1300
SELECT ROUND(1256.789, 2) AS two_places,
       ROUND(1256.789, 0) AS whole,
       ROUND(1256.789, -1) AS tens,
       ROUND(1256.789, -2) AS hundreds,
       ROUND(1250, -2) AS positive_tie,
       ROUND(-1250, -2) AS negative_tie;

The tens result is 1260 and hundreds is 1300. The exact halfway values 1250 and -1250 round to 1300 and -1300.

Check halfway values and input types

DECIMAL is a fixed-point type that stores exact decimal digits at a specified scale. At an exact halfway value, ROUND() chooses the result farther from zero. Floating-point types such as FLOAT and DOUBLE represent approximate values, and MariaDB uses the system library’s rounding behavior for them.

SELECT ROUND(CAST(2.5 AS DECIMAL(3, 1))) AS exact_pos,
       ROUND(CAST(-2.5 AS DECIMAL(3, 1))) AS exact_neg,
       ROUND(2.5E0) AS approximate_pos,
       ROUND(-2.5E0) AS approximate_neg,
       ROUND(NULL, 2) AS missing_value;

The exact values give 3 and -3. The exponent literals 2.5E0 and -2.5E0 use floating point. In the test on MariaDB 12.3.3 for Windows they gave 2 and -2, which may vary by platform. ROUND(NULL, 2) returns NULL.

When values that look the same round differently, check the source column type and the types used in intermediate calculations. If your calculation needs an exact decimal halfway rule, choose DECIMAL with suitable precision and scale at input.

Choose rounding, truncation, or formatting

Use ROUND() to pick the nearest value, TRUNCATE(X, D) to discard digits beyond the specified position, and FORMAT() to produce a display string with separators.

SELECT ROUND(1234.567, 2) AS rounded,
       TRUNCATE(1234.567, 2) AS truncated,
       FORMAT(1234.567, 2, 'en_US') AS displayed;

The rounded and truncated values are 1234.57 and 1234.56. The displayed result is the string 1,234.57 in the en_US locale. Keep data numeric for later totals and numeric sorting, and format it at the point of display.

Choose when to round a sum

Rounding each row before adding can produce a different total from rounding once at the end. Compare the two approaches with a pair of exact 0.005 amounts.

SELECT SUM(amount) AS raw_total,
       SUM(ROUND(amount, 2)) AS per_row,
       ROUND(SUM(amount), 2) AS after_sum
FROM (
    SELECT CAST(0.005 AS DECIMAL(4, 3)) AS amount
    UNION ALL
    SELECT CAST(0.005 AS DECIMAL(4, 3))
) AS sample_amounts;

The raw_total is 0.010. Rounding each amount first gives 0.01 per row and 0.02 in per_row. Rounding the original total gives 0.01 in after_sum. Choose the expression based on whether the report totals the displayed line amounts or rounds the original sum.