Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove comma from number format without removing decimal point in PHP

Tags:

php

I have this code to give $value a number format:

    $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. How can I do that?

like image 589
Errol Chaves Moya Avatar asked Oct 19 '25 02:10

Errol Chaves Moya


2 Answers

You have to assign the output of number_format to the variable:

$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:

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

Output:

1000000.00

Demo on 3v4l.org

like image 87
Nick Avatar answered Oct 21 '25 15:10

Nick


You can tell number_format what it should use as decimal-point and thousands-seperator! No need for the str_replace:

$value = 1000000.00;
echo number_format($value, 2, ".", "");
// output:
// 1000000.00
like image 34
Jeff Avatar answered Oct 21 '25 16:10

Jeff