简体   繁体   English

从数字格式中删除逗号而不删除 PHP 中的小数点

[英]Remove comma from number format without removing decimal point in PHP

I have this code to give $value a number format:我有这个代码给$value一个数字格式:

    $value=1000000.00;

    number_format($value,2);

    echo str_replace(",", "", $value)

But when I try to replace the comma, the decimals get removed.但是当我尝试替换逗号时,小数会被删除。 I want to print $value as it originally was: 1000000.00 .我想按原样打印$value1000000.00 How can I do that?我怎样才能做到这一点?

You have to assign the output of number_format to the variable:您必须将number_format的输出分配给变量:

$value=1000000.00;
$value = number_format($value,2);
echo str_replace(",", "", $value);

Output:输出:

1000000.00

You can also simply tell number_format not to use a thousands separator:您也可以简单地告诉number_format不要使用千位分隔符:

$value = number_format($value,2,'.','');
echo $value;

Output:输出:

1000000.00

Demo on 3v4l.org 3v4l.org 上的演示

You can tell number_format what it should use as decimal-point and thousands-seperator!您可以告诉number_format它应该使用什么作为小数点和千位分隔符! No need for the str_replace:不需要 str_replace:

$value = 1000000.00;
echo number_format($value, 2, ".", "");
// output:
// 1000000.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM