All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP 2 Decimal Places Without Rounding

Last Updated : Mar 11, 2024

PHP 2 Decimal Places Without Rounding

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>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪