Home > Backend Development > PHP Problem > How to achieve division with two decimal places in PHP

How to achieve division with two decimal places in PHP

青灯夜游
Release: 2023-03-15 12:08:02
Original
5701 people have browsed it

php method to implement division with two decimal places: 1. Use the "/" operator to perform division operation, the syntax is "value 1 / value 2"; 2. Use "number_format (result of division operation, 2 )" or "sprintf("%.2f", the result of division operation)" statement to retain two decimal places.

How to achieve division with two decimal places in PHP

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

php realizes division and retains two digits Decimals can be divided into two parts:

  • ##Dividing two numbers: You can use the "/" operator to perform division operations

  • Retain two decimal places: You can use the number_format() or sprintf() function to process the result of the division operation and retain two decimal places.

    • number_format() formats numbers by grouping thousands.

    • The sprintf() function writes a formatted string into a variable.

Example:


<?php
header("Content-type:text/html;charset=utf-8");
$a=10;
$b=3;
$c=$a/$b;
echo "两数相除的结果:".$c;
echo "<br>使用number_format()保留两位小数:".number_format($c, 2);
echo "<br>使用sprintf()保留两位小数:".sprintf("%.2f",$c);
?>
Copy after login

How to achieve division with two decimal places in PHP

Recommended learning: "

PHP Video Tutorial

The above is the detailed content of How to achieve division with two decimal places in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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