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

How to get 2 decimal places for integer division in php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-03-15 08:14:02
Original
2620 people have browsed it

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)
Copy after login

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);
?>
Copy after login

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!

Related labels:
php
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template