PHP 2 Decimal Places Without Rounding
Last Updated : Jan 1, 2023
IN - PHP
In this tutorial we will show you the solution of PHP 2 decimal places without rounding, today we are going to understand how to create a number having 2 decimal places without rounding in php.
If someone want to become a developer then he must have knowledge that how to play with numbers.
In this article, today we are going to done an interesting topic with number that is we have to generate a number having 2 decimal places without rounding in php.
Step By Step Guide On PHP 2 Decimal Places Without Rounding :-
Here, one thing to note here that there are many ways with help of which we are able to create a two decimal places in number without rounding.
The methods are by using number_format(), sprint() and floor() functions. There are all used separately also to done this single task.
Let us understand them with help of codes.
<!DOCTYPE html> <html> <head> <title> php two decimal places without rounding </title> </head> <body> <?php // first way echo number_format(47,2); // output is 47.00 // second way echo sprint(‘%.2f’, 60); // output is 60.00 // third way echo number_format(floor(0.98989898*100)/100,2); // output is 0.98 ?> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
- Here, as you can see that we use three method first one by using number_format(), next one is by using sprint() and last one is with help of a combination of number_format() and floor(). We can use them separately also.
- They all have different syntax. And we use echo to print them. We did not use a variable in which we store our value and they use if to apply function then print it.
- We directly echo the function as you can see and give values to them directly. You can choose your own way as what method do you want to print or show number.
- As we can see the in outputs we see the number with two decimals without rounding off. We also can use different variable or values from specified in this article.
- At last, the <body> and <html> tags are closed with </body> and </html> respectively.
At last in conclusion, here we can say that with the help of this article we are able to understand how to create a 2 decimal place number without rounding in php.
I hope this tutorial on PHP 2 decimal places without rounding helps you and the steps and method mentioned above are easy to follow and implement.