How to keep one decimal place in PHP without rounding

coldplay.xixi
Release: 2023-03-04 20:48:02
Original
3764 people have browsed it

PHP method of retaining one decimal point and not rounding: 1. Use the function [number_format], the code is [$total=number_format(2/3,1)]; 2. Use the function [$total_1 =sprintf( "%.1f ",2/3)].

How to keep one decimal place in PHP without rounding

[Related learning recommendations: php programming (video)]

PHP retains decimal points The last digit and not rounded method:

Commonly used methods are as follows:

<?php 
$total = 1225.49.5;
$total = floor( ($total*0.95)* 10 ) /10;      // $total = floor( $total * 9.5 ) /10;  这样写得到的值更精确            
$total = sprintf(&#39;%.1f&#39;, (float)$total); 
echo $total;
?>
Copy after login

Output result: 1225.4

php retains the decimal point Several methods for one person

<?php 
方法:$total = number_format(2/3,1);     // 1表示保留的是小数点后一位,会四舍五入
echo $total ."<br/>";
方法:$total_1 =sprintf( "%.1f ",2/3);      // 1f表示小数点后一位
echo $total_1 ."<br/>";
$total_2=printf( "%.1f ",2/3); 
echo $total_2."<br/>";
?>
Copy after login

Related learning recommendations:Programming video

The above is the detailed content of How to keep one decimal place in PHP without rounding. For more information, please follow other related articles on the PHP Chinese website!

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