Home  >  Article  >  Backend Development  >  How to get 2 decimal places for integer division in php

How to get 2 decimal places for integer division in php

WBOY
WBOYOriginal
2022-02-10 10:28:132419browse

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)".

How to get 2 decimal places for integer division in php

The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

How to take 2 decimal places for integer division in php

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:

How to get 2 decimal places for integer division in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn