In MySQL, the PI()
function returns the value of π (pi). The number π is a mathematical constant approximately equal to 3.14159 (although it can also be displayed with much greater precision than this).
The PI()
function displays π with a default precision of 7 (i.e. 3.141593), however MySQL uses the full double-precision value internally.
Syntax
The syntax goes like this:
PI()
So this function doesn’t require (or accept) any arguments.
Example 1 – Default Display
Here’s a basic example to demonstrate what PI()
returns.
SELECT PI();
Result:
+----------+ | PI() | +----------+ | 3.141593 | +----------+
In this case π is returned with a default precision of 7.
Example 2 – Increased Precision
Here’s an example of displaying π using greater precision.
SELECT PI()+0.000000000000000000;
Result:
+---------------------------+ | PI()+0.000000000000000000 | +---------------------------+ | 3.141592653589793000 | +---------------------------+
Example 3 – Reduced Precision
Here’s an example of displaying π using reduced precision. In this case we use the ROUND()
function to specify how many decimal places to return.
SELECT ROUND(PI(), 2);
Result:
+----------------+ | ROUND(PI(), 2) | +----------------+ | 3.14 | +----------------+
And if we round it to 4 decimal places the digit 5
will be rounded up to 6
.
SELECT ROUND(PI(), 4);
Result:
+----------------+ | ROUND(PI(), 4) | +----------------+ | 3.1416 | +----------------+