Home > Backend Development > PHP Tutorial > 如何保留小数点后的零?

如何保留小数点后的零?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-20 12:34:02
Original
2415 people have browsed it

假设有字符串$a = '8元';$b = '8.3'; 怎么统一变为$c = '8.0元'?


回复讨论(解决方案)

$b = '8.3';echo number_format(floor($b),1).' 元';
Copy after login
Copy after login

$b = '8.3';echo number_format(floor($b),1).' 元';
Copy after login
Copy after login



$a呢?

$a = '8元';echo preg_replace_callback('#(\d+)元#',create_function('$m','return number_format(floor($m[1]),1)." 元";'),$a);
Copy after login

你应该把数字单独存放格式化后拼接 “元”或其他字符

$a = '8元';$b = '8.3';echo intval($a) . '.0';echo intval($b) . '.0';
Copy after login

$c = sprintf('%.1f',intval($a));
Copy after login

$a =8.3;

//第一种
$c1 = number_format(floor($a),1);    
echo $c1 ."
";

//第二种
$c2  =sprintf( "%.1f ",floor($a));     
echo $c2 ."
";

$c3 =printf( "%.1f ",floor($a)); 
echo $c3 ."
";

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