Home > Article > Backend Development > How to get 2 decimal places for integer division in php
In PHP, you can use the "number_format()" function to take two decimal places for the result of integer division. This function is used to format numbers by thousands grouping. The setting parameters can specify how many decimal places are used. To format a number, the syntax is "number_format(result after integer division, 2)".
The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer
number_format() function formats numbers by thousands grouping.
Note: This function supports one, two or four parameters (not three).
Syntax
number_format(number,decimals,decimalpoint,separator)
The parameters are expressed as follows:
number is required. The number to format. If no other parameters are set, the number is formatted without a decimal point and with a comma (,) as the thousands separator.
decimals Optional. Specify the number of decimal places. If this parameter is set, numbers are formatted using a period (.) as the decimal point.
decimalpoint Optional. Specifies the string used as the decimal point.
separator Optional. Specifies the string used as thousands separator. Only the first character of the parameter is used. For example, "xxx" only outputs "x".
The example is as follows:
<?php $x=16; $y=8; $z=$x / $y; echo number_format($z, 2); ?>
Output result:
Recommended study:《 PHP video tutorial》
The above is the detailed content of How to get 2 decimal places for integer division in php. For more information, please follow other related articles on the PHP Chinese website!