How to remove the value after two decimal places in php

青灯夜游
Release: 2023-03-16 09:44:02
Original
2322 people have browsed it

4 methods: 1. Use the "round(floating point number, 2)" statement to round the floating point number and retain two decimal places; 2. Use the "number_format(number, 2)" statement; 3. Use the "sprintf(" .2f", number)" statement; 4. Use the "floor(number*100)/100" statement.

How to remove the value after two decimal places in php

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

Use php to remove the value after two decimal places, In other words, use PHP to retain two decimal places.

PHP provides a variety of methods. Let’s take a look:

1. Use the round() function

round() function rounds floating point numbers.

Just set the second parameter of the function to 2 to preserve two decimal places.

<?php 
echo round(3.412,2)."<br>"; 
echo round(3.45648,2); 
?>
Copy after login

How to remove the value after two decimal places in php

2. Use the number_format() function

The number_format() function is used to convert strings into numbers. It returns a formatted number on success, otherwise gives E_WARNING on failure.

To achieve rounding, if there is no decimal place, it will be automatically filled with 0 to the specified decimal place

<?php 
echo number_format(4.582,2)."<br>"; 
echo number_format(4.588,2)."<br>"; 
echo number_format(4.1,2); 
?>
Copy after login

How to remove the value after two decimal places in php

3, Use the sprintf() function

to achieve rounding, and if it does not have decimal places, it will automatically use the specified character (specified 0) to fill in the specified decimal places

<?php 
echo sprintf("%01.2f", 5.228)."<br>"; 
echo sprintf("%01.2f", 5.224)."<br>"; 
echo sprintf("%01.2f", 5);
?>
Copy after login

How to remove the value after two decimal places in php

4. Use the floor() function

to retain two decimal places without rounding.

The floor() function is rounded. For example, floor(4.66456) results in: 4, floor(9.1254) results in 9.

Therefore, to remove two decimal places, you need to multiply by 100 first, then round up, and then divide by 100, that is:

floor(数值*100)/100
Copy after login

Example:

<?php 
echo (floor(3.148*100)/100)."<br>"; 
echo (floor(3.142*100)/100); 
?>
Copy after login

How to remove the value after two decimal places in php

Recommended learning: "PHP Video Tutorial"

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

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!