It is very common that when programming a website or application with PHP we have to work with numbers, of which the most problematic are decimals, whether they are prices or other types of numerical data with greater precision. In these cases we usually need to adjust the value of these figures to several decimal places giving them format with PHP.
In this tutorial I am going to show you, through simple examples, how to format numbers in phpfor numbers with decimals, text-to-number transformations, and interchangeable use of comma or period.
Specifically, I am going to deal with the following types of formatting numbers:
- Rounding numbers with decimals or round to whole numbers. with function round() php.
- Setting decimal places of a number, its specific symbols for decimals and thousands. Through number_format() This is very common to use to display results in European/English/French format, etc.
- Transform a text type number into a numeric value.
Content
- Round PHP examples to round numbers
- Example of number_format() to format numbers
- Convert number_format() to number
Round PHP examples to round numbers
For round decimal numbers in PHP we will use the function round() from PHP.
Description of the Round function in PHP:
float round ( float $val, $precision, int $modo )
This function, by default, returns an integer and approximate upwards, that is:
<?php $number = 33.3; echo round ( $number ); //imprime 34 ?>
This function allows full configuration of the result to be returned, being able to indicate the precision (number of decimal places) that the returned number should have and, regarding the type of rounding, if we want rounding up or down.
Specifically, the options available to us are:
- PHP_ROUND_HALF_UP for round up.
- PHP_ROUND_HALF_DOWN for round down or the nearest previous number.
- PHP_ROUND_HALF_EVEN for round up to even number closest.
- PHP_ROUND_HALF_ODD for round up to the odd number nearest.
examples round with different types of rounding and precision:
<?php echo round(9.55, 0, PHP_ROUND_HALF_UP); // 10 echo round(9.55, 1, PHP_ROUND_HALF_UP); // 9.6 echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9 echo round(9.55, 1, PHP_ROUND_HALF_DOWN); // 9.5 echo round(7.5, 0, PHP_ROUND_HALF_EVEN); // 8 echo round(7.5, 0, PHP_ROUND_HALF_ODD); // 7 ?>
Example of number_format() to format numbers
While programming in PHP you will be able to modify integers by using the php number_format() function.
This function allows us to perform various conversions and changes to decimal or integer numbers. We will can:
- Go from a decimal number to an integer and vice versa with a simple call.
- indicate the exact precision required for a number (decimal places).
- Indicate the symbols that we want to use for the decimals (by default is the point) and for the thousands (by default the comma is used).
There are three uses of the function: with one parameter, with two, and with four:
- number_format ( float $number)
- number_format ( float $number, $number_decimals )
- number_format ( float $number, $number_places, $symbol_places, $symbol_thousands)
Let’s see examples of each use:
<?php $num = 1999.9; echo number_format($num); //imprime 1,999 echo number_format($num, 2); //imprime 1,999.90 echo number_format($num, 2, ",", ""); //imprime 1999,90 echo number_format($num, 1, ".", ""); //imprime 1999.90 echo number_format($num, 1, ".", " "); //imprime 1 999.90 ?>
Convert number_format() to number
Lastly, I would like to show you the quick and correct way to pass numbers in string format to a numeric value understandable by the system. With this transformation you will be able to start performing operations with this data, which would otherwise lead to errors.
We will do this transformation with 3 php functions for this purpose: intval(), floatval() and doubleval(). You must use the first php function to get integers and the second and third for decimal numbers.
echo intval('2'); //imprime 2 echo floatval('3.4'); //imprime 3.4 echo doubleval("3.4"); //imrpime 3.4 echo floatval( number_format("4", 2) ); // imprime 4.00
Note: Keep in mind that the number you can get will be limited by the type of system you use (32-bit or 64-bit). Being for 32 bits the range from -2147483648 to 2147483647.