Nuancing data
In addition to performing indexing and substituting on data, MySQL also allows for several ways of massaging the data to suit your needs. Some of the more common ones are discussed in this chapter.
ROUND()
As the name suggests, the mathematical function ROUND()
serves to round decimal values to a specified number of places. The base value comes first in the syntax:
SELECT ROUND(base value, number of positions);
So rounding an already rough approximation of Pi would look like this:
mysql> SELECT ROUND(22/7, 2) as PI; +------+ | PI | +------+ | 3.14 | +------+ 1 row in set (0.00 sec)
The ROUND()
function will accept whatever value you give it for the number of positions. However, if the number of places exceeds MySQL's built-in abilities to calculate a value, the extra places will be filled with zeroes:
mysql> SELECT ROUND(22/7, 20) as PI; +------------------------+ | PI | +------------------------+ | 3.14285714200000000000 | +------------------------+ 1 row in set...