PHP

FORMAT NUMBER AS CURRENCY

(quick examples)

THE NUMBER $amt = 6543.21;

FORMAT NUMBER

01

US DOLLARS - $6,543.21 $usd = "$" . number_format($amt, 2, ".", ",");

JAPANESE YEN - ¥6,543 $jpy = "¥" . number_format($amt, 0, ".", ",");

NUMBER_FORMAT(NUMBER, DECIMALS, THOUSANDS SEPARATOR, DECIMAL SEPARATOR)

THE NUMBER $amt = 6543.21;

REGULAR EXPRESSION

02

REGULAR EXPRESSION - “ADD A COMMA FOR EVERY 3 DIGITS” $regex = "/\B(?=(\d{3})+(?!\d))/i";

TO USD - $6,543.21 $usd = "$" . preg_replace($regex, ",", $amt);

THE NUMBER $amt = 6543.21;

NUMBER FORMATTER

03

US DOLLARS - $6,543.21 $nf = new NumberFormatter("en_US", NumberFormatter::CURRENCY); $usd = $nf->formatCurrency($amt, "USD");

JAPANESE YEN - ¥6,543 $nf = new NumberFormatter("ja_JP", NumberFormatter::CURRENCY); $jpy = $nf->formatCurrency($amt, "JPY");