数组值的选择数字格式


Selectional number formatting of array values

我有两种类型的数字(FLOAT和BIGINT)在mysql表

0.002:

B: 51443234

我需要将数字中的"。"替换为",",我可以通过number_format

来实现。
foreach ($row as $key => $val) { 
                if (is_numeric($val)) {
                        $row[$key] = number_format($val,'3', ',', '.'); }
                }
            }

新格式为:

: 0002

B: 51.443.234,000

但是我不希望数字B后面的零,所以我尝试分别格式化FLOAT和BIGINT值:

foreach ($row as $key => $val) { 
                if (is_numeric($val)) {
                        if (is_float($val)) {
                        $row[$key] = number_format($val,'3', ',', '.'); }
                        else {
                        $row[$key] = number_format($val,'0', ',', '.'); }
                }
            }

但是这不起作用

我不确定问题在哪里,如果我能做到这一点,而不将值转换为字符串。

(我也不知道转换成字符串的缺点是什么…)

你能告诉我正确的方法吗?谢谢你。

PHP是一种松散类型语言,因此它不知道 float bigint 。事实上,0,00251.443.234都不是有效值。

做计算时,不要改变任何东西。要显示你的数字,可以按你想要的方式格式化它们,即使它们是字符串。

1. <?php
2. $float = 2.25;
3. $bigInt = 4235234;
4.
5. $commaFloat = 2,25;
6. $commaBigInt = 4.235.234;
7. ?>
Parse error: syntax error, unexpected ',' on line 5
http://codepad.org/epIuX8MZ