Number::trim

Removes trailing zero digits after the decimal point, turning 12.30 into 12.3.

Signature
Number::trim(int|float $number): int|float
  • Added in Laravel 11.18.0

Parameters

ParameterTypeDescription
$$numberint|floatThe value to trim.

Return value

int|floatThe same value with any trailing zero digits after the decimal point removed. INF and NAN are returned unchanged.

Changelog

  • PHP 11.18.0Added to the Number class.
  • PHP 12.61.1INF and NAN are returned as-is instead of being converted to null by the internal json_encode round trip.

Number::trim() strips the meaningless zeros off the end of a decimal: 12.0 prints as 12, 12.30 as 12.3. Reach for it when a value came from a database decimal column or a calculation and you want 1.5 rather than 1.500000 in a label, an attribute or a JSON payload.

It returns a number, not a string

This is the point people miss. The signature is int|float in, int|float out. You cannot feed it the output of Number::format, because that is a string, and it will not remove trailing zeros from a formatted result — it operates on the value before formatting. It also does no rounding: 12.3456 comes back as 12.3456.

Why it needs to exist at all

Internally it round-trips the value through json_encode()/json_decode(), which uses PHP's shortest round-trip representation. That is a different precision setting from the one plain string conversion uses, so the returned float can occasionally carry more visible digits than the original did when echoed. For display, prefer Number::format($value, maxPrecision: n), which caps digits deterministically; use trim() when you need a numeric value tidied rather than a string built.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Tidying a decimal column such as 1.500 before echoing it in a product spec table
  • Normalising a calculated rate before encoding it into a JSON API response
  • Dropping a redundant .00 from a quantity shown next to a unit label

Frequently asked questions

Why does Number::trim not remove the zeros from my formatted string?

Because it takes and returns a number, not a string. Number::format() produces the string, so trim() has to run on the raw value before formatting — or use Number::format($value, maxPrecision: n), which omits trailing zeros itself.

Does Number::trim round the number?

No. It only removes trailing zeros after the decimal point, so 12.3456 comes back unchanged. Use Number::format() with maxPrecision, or PHP's round(), when you actually want fewer significant digits.

Which Laravel version added Number::trim?

Laravel 11.18.0. A later fix in 12.61.1 made INF and NAN pass through unchanged; before that they were converted to null by the internal json_encode round trip.

More examples

A quantity beside its unit

Prints 2.5 kg, 1 kg, 0.75 kg. Without the trim the middle row would read 1 anyway in PHP, but a value arriving from a decimal(8,3) column as the string '1.000' is cast to float first and then trimmed, which is the case this method is really for.

Example · php
<?php

use Illuminate\Support\Number;

$rows = [
    ['weight' => 2.500, 'unit' => 'kg'],
    ['weight' => 1.000, 'unit' => 'kg'],
    ['weight' => 0.750, 'unit' => 'kg'],
];

foreach ($rows as $row) {
    printf("%s %s\n", Number::trim($row['weight']), $row['unit']);
}

Trim the value, format the string

The first three lines print 1234.5, 1,234.50 and 1,234.5. The last line is the mistake to avoid: format() returns a string, so passing it to trim() is a TypeError under strict types and a coerced re-parse otherwise. Trim before formatting, never after.

Example · php
<?php

use Illuminate\Support\Number;

$value = 1234.5000;

echo Number::trim($value), PHP_EOL;
echo Number::format($value, precision: 2), PHP_EOL;
echo Number::format(Number::trim($value), maxPrecision: 4), PHP_EOL;
var_dump(Number::trim(Number::format($value, precision: 2)));

Special float values

Since Laravel 12.61.1 these come back as float(INF), float(-INF) and true. On earlier versions json_encode() failed on them and the method returned null, silently replacing an overflow with a missing value — worth knowing if you trim results of a division that can blow up.

Example · php
<?php

use Illuminate\Support\Number;

var_dump(Number::trim(INF));
var_dump(Number::trim(-INF));
var_dump(is_nan(Number::trim(NAN)));

Discussion

  • Be the first to comment on this lesson.