0

## php number_format 千分位問題!!!

Dear IT前輩們

``````\$test = "508224.00-";
\$test = number_format(\$test);
echo \$test;
``````

65199146.00
508224.00-
0.00
64690922.00
42837177.00-
21853745.00
3710279.00-
3766647.00-
5570543.00-
8806276.00
411581.00
36523350.00-
27305493.00-
0.00
27305493.00-

fillano iT邦超人 1 級 ‧ 2017-03-09 18:05:52 檢舉
php的數字格式，負數不是把符號擺在最後的吧...

fillano iT邦超人 1 級 ‧ 2017-03-10 13:51:03 檢舉

### 1 個回答

4
bizpro
iT邦大師 1 級 ‧ 2017-03-09 20:43:15

``````<?php
\$test = "508224.00-";
\$sign = 1;
if ( substr(\$test, -1) == "-" ) {
\$sign=-1;
}
\$test = number_format((float)\$test*\$sign,2);
echo \$test;
?>
``````

20170310T0949補充:

``````<?php
\$test = "508224.00-";
if ( substr(\$test, -1) == "-" ) {
\$test*=-1;
}
\$test = number_format(\$test,2);
echo \$test;
?>
``````

``````<?php
\$test = "508224.00-";
\$test*=(substr(\$test, -1) == "-"?-1:1);
\$test = number_format(\$test,2);
echo \$test;
?>
``````

wonton iT邦高手 6 級 ‧ 2017-03-10 08:32:21 檢舉

``````<?php
\$test = "508224.00-";
\$a = explode(".", \$test);
\$test = number_format(\$a[0]).".".\$a[1];
echo \$test;
?>
``````

Code是沒問題!!!

``````<table width="600" border="1.5" style="border-style:solid;">
<tr><td height="30px" bgcolor="#B0E0E6">NAME</td><td height="30px" bgcolor="#B0E0E6">DMBTR</td></tr>
<?
foreach (\$result["IT_FA01"] as \$AAA)
{

echo "<tr><td bgcolor='#E6E6FA'>"
, \$AAA["NAME"],"</td><td bgcolor='#E6E6FA'>",number_format(explode(".00", \$AAA["DMBTR"])),"</td></tr>";

echo \$AAA["DMBTR"];
}
?>

</table>
``````

https://pjchender.blogspot.tw/2015/05/php.html

bizpro iT邦大師 1 級 ‧ 2017-03-10 15:15:26 檢舉

Dear bizpro大大