不带逗号的 PHP 数字格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17028946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:08:47  来源:igfitidea点击:

PHP number format without comma

php

提问by Chris Muench

I want to display the number 1000.5 like 1000.50 with 2 decimal places and no commas/thousands separators.

我想显示数字 1000.5 像 1000.50 有 2 个小数位并且没有逗号/千位分隔符。

I am using number_format to achieve this:

我正在使用 number_format 来实现这一点:

number_format(1000.5, 2);

This results 1,000.50. The comma (,) separator appended in thousand place which is not required in the result.

结果为 1,000.50。逗号 (,) 分隔符附加在结果中不需要的千位。

How can I display the number with a trailing zero and no comma?

如何显示带有尾随零且没有逗号的数字?

回答by bwoebi

See the documentation for number_format: http://php.net/number_format

请参阅 number_format 的文档:http: //php.net/number_format

The functions parameters are:

函数参数为:

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

So use:

所以使用:

number_format(1000.5, 2, '.', '');

Which means that you don't use any (= empty string) thousands separator, only a decimal point.

这意味着您不使用任何(= 空字符串)千位分隔符,只使用小数点。

回答by Jason McCreary

number_format()takes additional parameters:

number_format()需要额外的参数:

number_format(1000.5, 2, '.', '');

The default is a period (.) for the decimal separator and a comma (,) for the thousands separator. I'd encourage you to read the documentation.

默认情况下.,小数点分隔符为句点 ( ,),千位分隔符为逗号 ( )。我鼓励您阅读文档

回答by Hauke P.

The documentation of number_formatcontains information about the parameter string $thousands_sep = ','. So this should work:

number_format文档包含有关参数的信息string $thousands_sep = ','。所以这应该有效:

number_format(1000.5, 2, '.', '');

回答by Binayak Das

Hi You can also use the below code to remove the comma from the number

您好您也可以使用以下代码从数字中删除逗号

<?php
$my_number = number_format(1000.5, 2);
echo str_replace(',', '', $my_number);
?>