SQL Numbers and Rounding
What you will learn
- How to round with
ROUND, and why you should - What
ABSdoes and when it helps - The difference between whole numbers and decimals, and how to see it with
TYPEOF - How to convert a value with
CAST - How to turn money in cents into a readable amount
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. ROUND
ROUND(number, places) rounds to the number of decimal places you give. Without the second argument it rounds to a whole number:
SELECT ROUND(2.567, 2) AS two_places, ROUND(2.567, 1) AS one_place, ROUND(2.567) AS whole, ROUND(-2.5) AS negative;
two_places one_place whole negative
---------- --------- ----- --------
2.57 2.6 3.0 -3.0
(1 row)
SELECT ROUND(2.567, 2) AS two_places, ROUND(2.567, 1) AS one_place, ROUND(2.567) AS whole, ROUND(-2.5) AS negative;
two_places one_place whole negative
---------- --------- ----- --------
2.57 2.6 3.0 -3.0
(1 row)
SELECT ROUND(2.567, 2) AS two_places, ROUND(2.567, 1) AS one_place, ROUND(2.567) AS whole, ROUND(-2.5) AS negative;
two_places one_place whole negative
---------- --------- ----- --------
2.57 2.6 3.0 -3.0
(1 row)
In SQLite a half is rounded away from zero (2.5 becomes 3.0, and -2.5 becomes -3.0), and the result of ROUND is a decimal number even when it ends in .0. Rounding rules for halves differ between databases, so do not rely on them when exactness matters.
Rounding also hides the tiny errors of lesson 1.3. In the Shop a margin printed as 2.6399999999999997 becomes 2.64:
SELECT name, price - cost AS raw_margin, ROUND(price - cost, 2) AS margin
FROM products ORDER BY product_id LIMIT 5;
name raw_margin margin
---------------- ------------------ ------
Steel pan 2.6399999999999997 2.64
Ceramic mug 13.89 13.89
Wooden spoon set 16.2 16.2
Glass jar 2.95 2.95
Chopping board 11.769999999999998 11.77
(5 rows)
SELECT account_id, balance_cents / 100.0 AS units, ROUND(balance_cents / 100.0, 1) AS one_place
FROM accounts ORDER BY account_id LIMIT 5;
account_id units one_place
---------- ------ ---------
1 1924.0 1924.0
2 520.78 520.8
3 627.8 627.8
4 999.0 999.0
5 1531.0 1531.0
(5 rows)
SELECT title, max_points * 0.35 AS raw, ROUND(max_points * 0.35, 1) AS rounded
FROM assignments ORDER BY assignment_id LIMIT 5;
title raw rounded
---------- ---- -------
Midterm 35.0 35.0
Project 35.0 35.0
Quiz 1 7.0 7.0
Homework 2 17.5 17.5
Homework 1 3.5 3.5
(5 rows)
Round only for display, at the end. If you round in the middle of a calculation and keep going, the small differences add up.
2. Percentages
A percentage is a part divided by the whole, times 100. Remember the lesson on whole-number division: multiply by 100.0 first so the result keeps its decimals:
SELECT name, price, cost, ROUND((price - cost) * 100.0 / price, 1) AS margin_percent
FROM products ORDER BY product_id LIMIT 5;
name price cost margin_percent
---------------- ----- ----- --------------
Steel pan 6.6 3.96 40.0
Ceramic mug 44.1 30.21 31.5
Wooden spoon set 29.5 13.3 54.9
Glass jar 5.4 2.45 54.6
Chopping board 26.4 14.63 44.6
(5 rows)
The share of each account balance in the total of the first five accounts:
SELECT account_id, balance_cents,
ROUND(balance_cents * 100.0 / (SELECT SUM(balance_cents) FROM accounts WHERE account_id <= 5), 1) AS share_percent
FROM accounts WHERE account_id <= 5 ORDER BY account_id;
account_id balance_cents share_percent
---------- ------------- -------------
1 192400 34.3
2 52078 9.3
3 62780 11.2
4 99900 17.8
5 153100 27.3
(5 rows)
The share of each assignment in the total points of the first five assignments:
SELECT title, max_points,
ROUND(max_points * 100.0 / (SELECT SUM(max_points) FROM assignments WHERE assignment_id <= 5), 1) AS share_percent
FROM assignments WHERE assignment_id <= 5 ORDER BY assignment_id;
title max_points share_percent
---------- ---------- -------------
Midterm 100 35.7
Project 100 35.7
Quiz 1 20 7.1
Homework 2 50 17.9
Homework 1 10 3.6
(5 rows)
3. ABS
ABS removes the minus sign. It is useful for a distance between two numbers, where only the size matters, not the direction:
SELECT ABS(-7) AS seven, ABS(7) AS also_seven, ABS(3 - 10) AS distance;
seven also_seven distance
----- ---------- --------
7 7 7
(1 row)
SELECT ABS(-7) AS seven, ABS(7) AS also_seven, ABS(3 - 10) AS distance;
seven also_seven distance
----- ---------- --------
7 7 7
(1 row)
SELECT ABS(-7) AS seven, ABS(7) AS also_seven, ABS(3 - 10) AS distance;
seven also_seven distance
----- ---------- --------
7 7 7
(1 row)
4. Whole numbers and decimals
SQLite remembers whether a value is a whole number (integer) or a decimal (real). TYPEOF shows which:
SELECT TYPEOF(7) AS a, TYPEOF(7.0) AS b, TYPEOF(7 / 2) AS c, TYPEOF(7 / 2.0) AS d, TYPEOF('7') AS e, TYPEOF(NULL) AS f;
a b c d e f
------- ---- ------- ---- ---- ----
integer real integer real text null
(1 row)
SELECT TYPEOF(7) AS a, TYPEOF(7.0) AS b, TYPEOF(7 / 2) AS c, TYPEOF(7 / 2.0) AS d, TYPEOF('7') AS e, TYPEOF(NULL) AS f;
a b c d e f
------- ---- ------- ---- ---- ----
integer real integer real text null
(1 row)
SELECT TYPEOF(7) AS a, TYPEOF(7.0) AS b, TYPEOF(7 / 2) AS c, TYPEOF(7 / 2.0) AS d, TYPEOF('7') AS e, TYPEOF(NULL) AS f;
a b c d e f
------- ---- ------- ---- ---- ----
integer real integer real text null
(1 row)
Reading it: 7 is an integer, 7.0 is a real, dividing two integers gives an integer (lesson 1.3), and a number inside quotes is text, not a number.
5. CAST
CAST(value AS type) converts a value to another type. The common targets are INTEGER, REAL and TEXT:
SELECT CAST(7.9 AS INTEGER) AS cut_off, CAST('42' AS INTEGER) AS from_text, CAST(5 AS REAL) AS as_decimal, CAST(7 AS TEXT) || '!' AS as_text;
cut_off from_text as_decimal as_text
------- --------- ---------- -------
7 42 5.0 7!
(1 row)
SELECT CAST(7.9 AS INTEGER) AS cut_off, CAST('42' AS INTEGER) AS from_text, CAST(5 AS REAL) AS as_decimal, CAST(7 AS TEXT) || '!' AS as_text;
cut_off from_text as_decimal as_text
------- --------- ---------- -------
7 42 5.0 7!
(1 row)
SELECT CAST(7.9 AS INTEGER) AS cut_off, CAST('42' AS INTEGER) AS from_text, CAST(5 AS REAL) AS as_decimal, CAST(7 AS TEXT) || '!' AS as_text;
cut_off from_text as_decimal as_text
------- --------- ---------- -------
7 42 5.0 7!
(1 row)
CAST(7.9 AS INTEGER) gives 7: it cuts off the decimals, it does not round. To round, use ROUND first.
SELECT name, price, CAST(price AS INTEGER) AS cut_off, ROUND(price) AS rounded
FROM products ORDER BY product_id LIMIT 5;
name price cut_off rounded
---------------- ----- ------- -------
Steel pan 6.6 6 7.0
Ceramic mug 44.1 44 44.0
Wooden spoon set 29.5 29 30.0
Glass jar 5.4 5 5.0
Chopping board 26.4 26 26.0
(5 rows)
Whole units from cents, cutting off the cents, and then rounded:
SELECT account_id, balance_cents, CAST(balance_cents / 100.0 AS INTEGER) AS cut_off, ROUND(balance_cents / 100.0) AS rounded
FROM accounts ORDER BY account_id LIMIT 5;
account_id balance_cents cut_off rounded
---------- ------------- ------- -------
1 192400 1924 1924.0
2 52078 520 521.0
3 62780 627 628.0
4 99900 999 999.0
5 153100 1531 1531.0
(5 rows)
SELECT title, max_points * 0.35 AS raw, CAST(max_points * 0.35 AS INTEGER) AS cut_off, ROUND(max_points * 0.35) AS rounded
FROM assignments ORDER BY assignment_id LIMIT 5;
title raw cut_off rounded
---------- ---- ------- -------
Midterm 35.0 35 35.0
Project 35.0 35 35.0
Quiz 1 7.0 7 7.0
Homework 2 17.5 17 18.0
Homework 1 3.5 3 4.0
(5 rows)
Worked example: show money held in cents
The Bank keeps balance_cents = 192450. The report wants 1924.50.
| Step | Expression | Result |
|---|---|---|
| Divide by a decimal 100 | 192450 / 100.0 |
1924.5 |
| Round to two places | ROUND(192450 / 100.0, 2) |
1924.5 |
| Check the type | TYPEOF(ROUND(192450 / 100.0, 2)) |
real |
A decimal number prints without trailing zeros (1924.5), so a program that must show 1924.50 formats the number when it displays it. Storing money as whole cents and converting only at the end is how real systems avoid rounding errors. The amount is made up.
Common mistakes
- Rounding too early, then calculating with the rounded value.
- Confusing
CAST ... AS INTEGERwith rounding.CASTcuts off the decimals. - Dividing whole numbers for a percentage. Multiply by
100.0first. - Treating a number in quotes as a number.
'7'is text; useCASTwhen you need a number. - Trusting how a half is rounded across different databases.
Key takeaways
ROUND(x, places)rounds, and it is for display, at the end.ABSremoves the sign;TYPEOFshows whether a value is integer, real, text or null.CAST(x AS type)converts;CAST ... AS INTEGERcuts off decimals.- For percentages and money, use a decimal such as
100.0in the calculation.
Practice
1. What does ROUND(3.14159, 2) return?
Show answer
3.14.
2. What does CAST(9.99 AS INTEGER) return, and why is it not 10?
Show answer
9. CAST cuts off the decimals; it does not round. Use ROUND(9.99) for 10.0.
3. What does ABS(5 - 12) return?
Show answer
7, the distance between the numbers.
4. What does TYPEOF('5') return?
Show answer
text. A number in quotes is text.
5. Write an expression for the percentage that 30 is of 120, rounded to one place.
Show answer
ROUND(30 * 100.0 / 120, 1), which is 25.0.
6. Why is it better to round at the end of a calculation than in the middle?
Show answer
Each rounding throws away a little; if you keep calculating with the rounded value the small errors add up.