Home >Backend Development >PHP Problem >How to divide by 100 in php with two decimal places

How to divide by 100 in php with two decimal places

青灯夜游
青灯夜游Original
2022-04-22 18:23:273427browse

How to divide php by 100 with two decimal places: 1. Use the "/" operator to perform division operation, the syntax is "numeric value / 100"; 2. Use "number_format (division result, 2)" or " The sprintf("%.2f",division result)" statement rounds the value and retains two decimal places.

How to divide by 100 in php with two decimal places

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

php divided by 100 Method of retaining two decimal places

In PHP, you can use the "/" operator to perform division operations to divide a number by 100

<?php
header("Content-type:text/html;charset=utf-8");
$a=332.5654;
$b=100;
$c=$a/$b;
echo "一个数除以100的结果:".$c;
?>

How to divide by 100 in php with two decimal places

It can be seen that if the dividend is a floating point number, its decimal point has several digits, so how to retain two decimal places?

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

  • number_format()Numbers can be formatted by thousands grouping.

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

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

How to divide by 100 in php with two decimal places

Note: The number_format() or sprintf() function will round when processing floating point numbers.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to divide by 100 in php with two decimal places. 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