【发布时间】:2014-04-29 01:10:51
【问题描述】:
我有一张表格,上面有这样的数字:
19809.1
1982.0
1982.19
它的货币。有没有我可以用来格式化这些数字的函数?
19 809,10
1 982,00
1 982,19
【问题讨论】:
-
@Blaine:我一直觉得
money_format()比number_format()更令人困惑。
标签: php number-formatting
我有一张表格,上面有这样的数字:
19809.1
1982.0
1982.19
它的货币。有没有我可以用来格式化这些数字的函数?
19 809,10
1 982,00
1 982,19
【问题讨论】:
money_format() 比number_format() 更令人困惑。
标签: php number-formatting
number_format 很容易做到这一点:
number_format(value, number_of_decimals, "decimal_separator", "thousand_separator")
类似这样的:
echo number_format(19809.1, 2, ",", " ");
这告诉数字有 2 位小数,逗号 , 作为小数分隔符,空格 作为千位分隔符。输出将是:
19 809,10
其他例子:
echo number_format(19809.1, 0, "", ".");
> 19.809
> no decimal and . as thousand separator
echo number_format(19809.1, 3, ".", ",");
> 19,809.100
> 3 decimals, comma as thousand separator and dot as decimal separator
【讨论】:
<kbd> 元素/标签用于表示用户输入。这不是让链接看起来更漂亮或“样式化”您的帖子的方法。
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
【讨论】:
来自http://www.php.net/manual/es/function.number-format.php
// notación francesa
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
【讨论】:
我会选择number_format函数:
$val=array(19809.1, 1982.0, 1982.19);
foreach ($val as $v)
echo number_format($v, 2, ',', ' ');
_ _ _ _
^ ^ ^ ^
value | | |
decimal positions | |
decimal separator |
thousands separator
返回:
19 809,10
1 982,00
1 982,19
【讨论】: