How to strip trailing zeros in PHP

How to strip trailing zeros in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Strip Trailing Zeros in PHP

When parsing JSON or processing API responses, you often encounter floating-point numbers represented as strings with excessive precision. A common example is geographic coordinates like 37.422005000000000000000000000000. While this data might be valid for internal calculations, displaying it directly to a user or logging it looks messy. The goal is to reduce 37.422005000000000000000000000000 back to the meaningful 37.422005 without altering the significant digits or removing zeros that are part of the number's value.

A naive approach is to use str_replace('0', '', $value). While this achieves a quick visual cleanup, it fundamentally breaks the numeric representation. If you apply str_replace to the string 37.422005, every zero inside the sequence gets deleted, resulting in 37.4225. This is incorrect because the trailing zeros after the decimal point were padding that needed removal, but the internal zeros defining the magnitude of the number were not.

The correct solution involves converting the string to a floating-point number first, then formatting it back into a string with controlled precision. PHP's number_format() function provides the most direct way to handle this, although it has specific constraints you must understand.

To strip trailing zeros while preserving significant digits, follow this pattern:

$value = '37.422005000000000000000000000000';
$stripped = number_format((float)$value, 10, '.', '');

Here is how the logic works:

  1. Type Casting: (float)$value converts the string into a numeric type. This triggers PHP's internal floating-point arithmetic rules. In most standard configurations, PHP floats maintain roughly 14-15 significant digits. The massive tail of zeros in the input string is effectively truncated during this conversion to 37.422005.
  2. Formatting: number_format() takes three key arguments: the number, the number of decimals to keep (or -1 for auto), and the decimal separator. By passing a high number like 10 as the precision argument, you tell PHP to round the value to that many decimal places. Since the float conversion already dropped the trailing zeros, number_format simply ensures the output string doesn't contain them. The final empty string parameter '' removes any thousands separators if they were present, though this is less critical for coordinates.

However, there are trade-offs to consider with this approach. Floating-point numbers in PHP (and IEEE 754 standard in general) have limited precision. If your input requires more than 14-15 significant digits—common in high-frequency trading or scientific computing—the simple cast to float will introduce rounding errors before you even get to the formatting step.

If you need to preserve extreme precision and simply format the output, consider using the bcmath extension which works with arbitrary precision numbers:

$value = '37.422005000000000000000000000000';
$stripped = bcformat($value, 10);

Note that bcformat is not always available in standard PHP distributions without enabling the extension or installing it via PECL. If bcmath is unavailable, stick to the float conversion method but be aware that you are accepting a small loss of precision for the sake of readability.

Another potential pitfall involves scientific notation. If your number is extremely large or small, casting to float might convert it to 3.7e-05. While number_format handles this well, you must ensure the output format matches your UI requirements. Always test the result with edge cases like negative numbers and numbers where the trailing zeros are fewer than the precision limit.

For Laravel projects specifically, if you are dealing with JSON responses from external APIs, you might handle this logic in a helper class or middleware to normalize data before it reaches your views or database. This keeps your core business logic clean and ensures consistent presentation across your entire application stack.

Our network