Number::abbreviate

Shortens a large number to compact notation, turning 1337 into 1K and 1230000 into 1.23M.

Signature
Number::abbreviate(int|float $number, int $precision = 0, ?int $maxPrecision = null): string|false
  • Added in Laravel 10.33.0

Parameters

ParameterTypeDescription
$$numberint|floatThe value to abbreviate. Negatives are handled and keep their sign.
$$precisionoptionalint= 0Exact fraction digits in the mantissa, padded with zeros.
$$maxPrecisionoptional?int= nullUpper bound on fraction digits. Takes priority over $precision.

Return value

string|falseThe abbreviated number with a K, M, B, T or Q suffix, or the plain formatted number below 1000.

Errors

Throws RuntimeException when the intl PHP extension is not loaded, because the mantissa is rendered by Number::format().

Changelog

  • PHP 10.33.0Introduced together with the Illuminate\Support\Number class.
  • PHP 10.40.0A float zero now returns '0' instead of falling through the exponent maths.

Number::abbreviate() is Number::forHumans() with the suffix set swapped for single letters: K, M, B, T, Q at 10^3, 10^6, 10^9, 10^12 and 10^15. Reach for it when a metric tile has to fit a fixed width and the reader only needs the magnitude — follower counts, page views, revenue headlines.

It rounds, and the rounding hides a lot

With the default precision of 0, 1999 renders as 2K and 1500000 as 2M. That is fine on a tile and misleading in a report. If the exact figure matters, put it in a title attribute or a tooltip using Number::format() and keep the abbreviation purely visual.

Edges worth knowing

Anything under 1000 comes back with no suffix at all, so 999 is 999 rather than 1K. Negatives are formatted through abs() and get their minus sign put back. Beyond 10^18 the value is divided down recursively and keeps stacking the Q suffix. And although the unit maths is pure PHP, the mantissa is rendered by Number::format(), so intl is still required.

Example

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

When to use it

  • Fitting a follower or view count into a fixed-width metric card
  • Labelling chart axis ticks where the exact figure lives in the tooltip
  • Compressing a revenue headline on a mobile dashboard

Frequently asked questions

Why does Number::abbreviate(1999) return 2K?

Because the default precision is 0, so 1.999 is rounded to 2 before the suffix is appended. Pass precision or maxPrecision to keep the fraction, for example maxPrecision: 1 for 2K to become 2K and 1500 to become 1.5K.

What is the difference between abbreviate and forHumans?

Only the suffixes. abbreviate() calls forHumans() with abbreviate: true, giving K, M, B, T, Q; plain forHumans() spells out ' thousand', ' million' and so on. Rounding and parameters are otherwise identical.

Why does 999999 abbreviate to 1000K instead of 1M?

The exponent is chosen from the un-rounded value, so 999999 is bucketed at 10^3 and only then rounded to 1000. If that bothers you, round the value to your display precision before abbreviating it.

More examples

Where the suffix changes

999 stays bare, 1000 becomes 1K, 999999 rounds up to 1000K rather than jumping to 1M because the exponent is floored before rounding, and 2.4 trillion becomes 2T or 2.4T. That 1000K row is the one that surprises people.

Example · php
<?php

use Illuminate\Support\Number;

$values = [999, 1000, 1200, 999999, 1000000, 1500000000, 2400000000000];

foreach ($values as $value) {
    printf("%-15s %-6s %s\n", $value, Number::abbreviate($value), Number::abbreviate($value, maxPrecision: 2));
}

Precise value in the tooltip

Renders <span title="1,487,312 views">1.5M</span>. The tile stays narrow while the exact number is one hover away, which is the pattern to reach for whenever the abbreviated figure could be misread as the real one.

Example · php
<?php

use Illuminate\Support\Number;

$views = 1487312;

$html = sprintf(
    '<span title="%s views">%s</span>',
    Number::format($views),
    Number::abbreviate($views, maxPrecision: 1)
);

echo $html, PHP_EOL;

Abbreviating through forHumans

Both print 1.23M and the comparison is true: abbreviate() is literally a call to forHumans() with abbreviate set. Knowing that explains why the two share every rounding quirk and why neither accepts a locale.

Example · php
<?php

use Illuminate\Support\Number;

$n = 1230000;

echo Number::abbreviate($n, precision: 2), PHP_EOL;
echo Number::forHumans($n, precision: 2, abbreviate: true), PHP_EOL;
var_dump(Number::abbreviate($n, 2) === Number::forHumans($n, 2, abbreviate: true));

Discussion

  • Be the first to comment on this lesson.