Categories

Tools

bitdefender

1&1 Web Hosting

Example of how to use number_format() in PHP


The followingPHP codeshows a simpleuse ofnumber_format() function, very usefulwhen you need tocorrectly formatnumbers.

<?php
$total = 3456789;
$quantity = 34;
$price = $total/$quantity;
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Number format test - PHP</title>
    </head>
    <body>
        <div id="container">
			<?php
			echo $price;
			echo "<br />";
			echo number_format($price);
			echo "<br />";
			echo number_format($price, 1, ',', '.');
			echo "<br />";
			echo number_format($price, 2, ',', '.');
			echo "<br />";
			echo number_format($price, 3, ',', '.');
			echo "<br />";
			echo number_format($price, 4, ',', '.');
			echo "<br />";
			?>
        </div>
    </body>
</html>

Here is the resultof the example:

101670.26470588
101,670
101.670,3
101.670,26
101.670,265
101.670,2647
Posted in PHP by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.