How to reverse number_format() function in PHP
  John Mwaniki /   06 Dec 2023

How to reverse number_format() function in PHP

When dealing with large numbers, there is a widely adopted convention to insert commas in them. These commas serve as visual delimiters, breaking down lengthy numerical sequences into more manageable segments. This makes it easier for human eyes to grasp the value of a number, preventing confusion and errors during manual reading or data entry.

In PHP, the number_format() function is used for formatting numbers with commas.

However, having commas in numeric strings can pose challenges when performing arithmetic operations, especially in programming languages, such as PHP.

Attempting to perform calculations on numbers containing commas will result in errors. This is because PHP treats the commas as non-numeric characters rather than integral parts of the number, leading to incorrect calculations and errors.

For instance, let's say you are trying to add two numbers written as "1,500" and "2,000".

$num1 = "1,500";
$num2 = "2,000";
$result = $num1 + $num2;

This gives an incorrect answer (3) and results to notices below:

Notice: A non well formed numeric value encountered in /path/to/file.php on line x
Notice: A non well formed numeric value encountered in /path/to/file.php on line x

PHP interprets this as attempting to add the strings "1,500" and "2,000," leading to an unexpected result.

To overcome this problem, it's essential to remove the commas from numeric strings before engaging in arithmetic operations. This process essentially reverses the effect of the number_format() function, restoring the numbers to their original form.

Fortunately, PHP offers several methods to achieve this task.

Removing Commas from Numbers in PHP

There are several approaches to strip away the commas from numbers in PHP. Below we cover some of them:

1. Using str_replace() Function

The str_replace() function is used to replace a substring (specific characters) within a string with another string.

In this case, we use it to replace all instances of commas with an empty string, effectively removing them from the numeric string.

$withCommas = "1,000";
$withoutCommas = str_replace(',', '', $withCommas);

echo "Number before: ".$withCommas."<br>";
echo "Number after: ".$withoutCommas;

Output:
Number before: 1,000
Number after: 1000

You can then use the resultant string for arithmetic operations without errors.

2. Using preg_replace() Function

For a more advanced solution, you can use the preg_replace() function. It offers pattern matching capabilities, allowing for more precise control over comma removal.

$withCommas = "1,000";
$withoutCommas = preg_replace('/\D/', '', $withCommas);

echo "Number before: ".$withCommas."<br>";
echo "Number after: ".$withoutCommas;

Output:
Number before: 1,000
Number after: 1000

This regular expression removes all non-numeric characters, including commas, providing a clean numeric string.

Conclusion

Commas play a significant role in enhancing the readability of large numbers, making them easier to comprehend and interpret. However, when performing arithmetic operations in programming languages, commas can pose challenges and lead to errors due to their interpretation as separators.

To overcome this, it's essential to remove the commas before engaging in calculations. By understanding the concepts in this article, you can now effectively remove commas from numbers in PHP and thus enable accurate arithmetic operations.