PI()
function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.
Syntax :
PI()
Parameter :
This method does not accept any parameter.
Returns :
It returns the Pi value i.e. 3.141593.
Example-1 :
Returning the default value of Pi using PI Function.
SELECT PI() AS DefaultPiValue;
Output :
Example-2 :
Returning the value of Pi up to 18 decimal places using PI Function .
SELECT PI()+0.000000000000000000
AS PiValue;
Output :
PiValue |
---|
3.141592653589793000 |
Example-3 :
Using PI Function to calculate the area and perimeter of all circles in a column. To demonstrate, let us create a table named Circle.
CREATE TABLE Circle(
Circle_id INT AUTO_INCREMENT,
Radius DECIMAL(10, 3) NOT NULL,
PRIMARY KEY(Circle_id )
);
Now, insert some data to the Circle table.
INSERT INTO Circle(Radius )
VALUES
(2 ),(3),(10 ),(12.5 ),(6.80),
(4.60 ),(6),(20),(25) ;
So, the Circle Table is as follows.
SELECT * FROM Circle;
Circle_id | Radius |
---|
1 | 2.000 |
2 | 3.000 |
3 | 10.000 |
4 | 12.500 |
5 | 6.800 |
6 | 4.600 |
7 | 6.000 |
8 | 20.000 |
9 | 25.000 |
Now, we will calculate the area and perimeter of every circle using PI function.
SELECT Circle_id, Radius,
PI() * Radius * Radius AS Area,
2 * PI() * Radius AS Perimeter
FROM Circle;
Output :
Circle_id | Radius | Area | Perimeter |
---|
1 | 2.000 | 12.566371 | 12.566371 |
2 | 3.000 | 28.274334 | 18.849556 |
3 | 10.000 | 314.159265 | 62.831853 |
4 | 12.500 | 490.873852 | 78.539816 |
5 | 6.800 | 145.267244 | 42.725660 |
6 | 4.600 | 66.476101 | 28.902652 |
7 | 6.000 | 113.097336 | 37.699112 |
8 | 20.000 | 1256.637061 | 125.663706 |
9 | 25.000 | 1963.495408 | 157.079633 |
Similar Reads
SIGN() Function in MySQL SIGN() function in MySQL is used to return the sign of the given number. It returns 1 if the number is positive, -1 if the number is negative and 0 for zero. Syntax : SIGN(X) Parameter : SIGN() function accepts one parameter as input and will give you the results in values like Positive(+1),Negative
1 min read
TRIM() Function in MySQL TRIM() function in MySQL is used to clean up data. It is also used to remove the unwanted leading and trailing characters in a string. Syntax : TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) Parameter : This method accepts three-parameter as mentioned above and described below : BOTH | LEADIN
2 min read
TIME() Function in MySQL The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read
TAN() Function in MySQL TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
SUM() Function in MySQL The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
RIGHT() Function in MySQL RIGHT() function in MySQL is used to extract a specified number of characters from the right side of a given string. Second argument is used to decide, how many characters it should return. Syntax : RIGHT( str, len ) Parameter : This function accepts two parameter as mentioned above and described be
3 min read