Home  >  Article  >  Backend Development  >  How to keep one decimal place in PHP without rounding

How to keep one decimal place in PHP without rounding

coldplay.xixi
coldplay.xixiOriginal
2020-08-20 11:27:503612browse

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:

Output result: 1225.4

php retains the decimal point Several methods for one person

";
方法:$total_1 =sprintf( "%.1f ",2/3);      // 1f表示小数点后一位
echo $total_1 ."
"; $total_2=printf( "%.1f ",2/3); echo $total_2."
"; ?>

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!

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