Instruction
Good. Another function to practice is TRUNC(). It always rounds towards zero. As with ROUND(), there are two variants of TRUNC():
- The single-argument
TRUNC(x)takes a number of typenumericordouble precision. - The two-argument
TRUNC(x, p)takes an additional integerpargument which defines the number of decimal places.
Take a look at the example:
SELECT name, TRUNC(weight) AS weight_no_decimal_places, TRUNC(weight, 1) AS weight_one_decimal_place FROM character;
But there's a catch! The same which was true for the ROUND() function. The query above won't work because you may define the number of decimal places only for the numeric types, so you should cast the value:
SELECT name, TRUNC(weight) AS weight_no_decimal_places, TRUNC(weight::numeric, 1) AS weight_one_decimal_place FROM character;
Result:
| name | weight_no_decimal_places | weight_one_decimal_place |
|---|---|---|
| Kav | 65.0 | 65.0 |
| Gniok | 101.0 | 101.4 |
| ... | ... | ... |
Exercise
Show the character's name together with its account_balance, and the account balance truncated to one decimal place as the truncated_balance.
Stuck? Here's a hint!
You'll need to convert real data type to numeric:
TRUNC(account_balance::numeric, 1) AS truncated_balance



