Stripping Trailing Zeros from MySQL DECIMAL Values in PHP

I had a MySQL DECIMAL field with a precision of 3 decimals, but many values only had one decimal (e.g., 5.2). When I fetched the value, it came back as 5.200, and I wanted to display just 5.2 unless there was real precision beyond one decimal (like 5.211).

I tried using round() with 3 decimal places, and it worked—PHP’s float representation naturally drops trailing zeros. For example:

$value = 5.200;
echo round($value, 3); // outputs 5.2
$value2 = 5.211;
echo round($value2, 3); // outputs 5.211

It strips the trailing zeros without affecting the real precision. Just be aware that round() returns a float, and floating-point arithmetic can sometimes introduce tiny errors (e.g., 5.200 might become 5.199999…), though for most cases it’s fine.

If you want a more controlled approach, you can cast to float directly: echo (float) $value; or use sprintf('%g', $value) which automatically formats to the shortest representation without trailing zeros.

Topic Summary: Use round($value, 3) to strip trailing zeros from MySQL DECIMAL values like 5.200 → 5.2, preserving real precision. Alternatives: (float) $value or sprintf('%g', $value).

:hammer_and_wrench: Featured GitHub Resource:

  • ohmyzsh/ohmyzsh - :upside_down_face: A delightful community-driven (with 2,500+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, … (★ 188665)

:open_book: Topic Overview (Wikipedia):

PHP is a general-purpose scripting language geared towards web development. It was created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. PHP was originally an abbreviation of Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor.
Read more on Wikipedia

:movie_camera: YouTube Video:

---
title: Strip Trailing Zeros from DECIMAL
---
flowchart TD
A["Fetch DECIMAL from MySQL"] --> B["Strip Trailing Zeros"]
B --> C["Output Clean Value"]

round() works, but keep in mind it may produce floating-point artifacts. A more robust approach is to treat the value as a string and manipulate it directly.

Using sprintf('%g', $value)
This converts the number to a string with no trailing zeros and no decimal point if the number is whole. For example:

echo sprintf('%g', 5.200); // 5.2
echo sprintf('%g', 5.211); // 5.211
echo sprintf('%g', 5.000); // 5

But %g may switch to scientific notation for very large or very small numbers. If that’s a concern, use rtrim:

$formatted = rtrim(rtrim($value, '0'), '.');

This first removes trailing zeros, then removes the decimal point if nothing follows it. It’s simple and predictable.

For more control over decimal places (like always showing at least one decimal), you can combine number_format() with a dynamic precision detection, but that’s overkill for most cases.

I’d recommend sprintf('%g') for most scenarios—just be aware of the scientific notation edge case.