Home >Backend Development >PHP Problem >How to keep one decimal place in PHP without rounding
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)].
[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('%.1f', (float)$total); echo $total; ?>
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/>"; ?>
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!