Skip to content

Novis\Intl\NumberFormat::CompactDisplay

How NumberFormat::compact writes a large number in a short form.

NumberFormat::CompactDisplay is declared in the class Novis\Intl\NumberFormat.

You pass a NumberFormat::CompactDisplay as display. Short, the default, writes "1.2K" and "3.4M" in English. Long writes "1.2 thousand" and "3.4 million". Both round the number to a few digits, so use them for counts a person reads at a glance, such as followers or views.

Each locale has its own short forms. German writes "1,2 Mio." for 1.2 million, and does not shorten numbers below ten thousand in the Short form.

Examples

Short and long counts

<?nvs
// Shows a follower count written short and long with `NumberFormat::CompactDisplay`.
use Novis\Intl\NumberFormat;
array<int> $followers = [1200, 3400000];
// `Short` is the default and fits a small label.
echo Core\Str::join(NumberFormat::compact($followers, 'en', { display: NumberFormat::CompactDisplay::Short }), ' | '), "\n";
// `Long` writes the word in full.
echo Core\Str::join(NumberFormat::compact($followers, 'en', { display: NumberFormat::CompactDisplay::Long }), ' | '), "\n";
Output
1.2K | 3.4M
1.2 thousand | 3.4 million