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

How to achieve division with two decimal places in PHP

青灯夜游
青灯夜游 Original
2022-02-18 19:05:05 5558browse

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:


使用number_format()保留两位小数:".number_format($c, 2); echo "
使用sprintf()保留两位小数:".sprintf("%.2f",$c); ?>

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!

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