number formatter php code example

Example 1: php number format

$num = 12345.6789;
echo number_format($num, 2, '.', '') //12345.67
echo number_format($num, 3, ',', '.') //12.345,678

Example 2: number format php

<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>

Example 3: numberformatter php

/* PHP class to format numbers, currencies, percentages et cetera according
to the specified or default locale class. One must install and enable PHP
intl extension to utilize the NumberFormatter class. */

$num = 1234567.891234567891;

$nf = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
echo $nf->format($num);

$nf = new NumberFormatter('uk_UA', NumberFormatter::CURRENCY);
echo $nf->formatCurrency($num, 'UAH');

$nf = new NumberFormatter('lt_LT', NumberFormatter::SPELLOUT);
echo $nf->format($num);

$nf = new NumberFormatter('en_US', NumberFormatter::DURATION);
echo $nf->format($num);

$nf = new NumberFormatter('fr_FR', NumberFormatter::DECIMAL);
$nf->setPattern('#,##0.### kg');
echo $nf->format($num);

Tags:

Php Example