note 103030 added to class.numberformatter

From: Date: Mon, 21 Mar 2011 18:59:03 +0000
Subject: note 103030 added to class.numberformatter
Groups: php.notes 
Request: Send a blank email to php-notes+get-178058@lists.php.net to get a copy of this message
this class seems to be painful: it is not, formatting and parsing are highly customizable, but what you probably need is really simple: if you want to localize a number use: $a = new \NumberFormatter("it-IT", \NumberFormatter::DECIMAL); echo $a->format(12345.12345) . "<br>"; // outputs 12.345,12 $a->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); $a->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 100); // by default some locales got max 2 fraction digits, that is probably not what you want echo $a->format(12345.12345) . "<br>"; // outputs 12.345,12345 if you want to print money use: $a = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); echo $a->format(12345.12345) . "<br>"; // outputs €12.345,12 if you have money data stored as (for example) US dollars and you want to print them using the it-IT notation, you need to use $a = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); echo $a->formatMoney(12345, "USD") . "<br>"; // outputs $ 12.345,00 and it is formatted using the italian notation (comma as decimal separator) another useful example about currency (how to obtain the currency name by a locale string): $frontEndFormatter = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); $adminFormatter = new \NumberFormatter("en-US", \NumberFormatter::CURRENCY); $symbol = $adminFormatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL); // got USD echo $frontEndFormatter->formatMoney(12345.12345, $symbol) . "<br>"; ---- Server IP: 81.88.49.31 Probable Submitter: 87.7.150.203 ---- Manual Page -- http://www.php.net/manual/en/class.numberformatter.php Edit -- https://master.php.net/note/edit/103030 Del: integrated -- https://master.php.net/note/delete/103030/integrated Del: useless -- https://master.php.net/note/delete/103030/useless Del: bad code -- https://master.php.net/note/delete/103030/bad+code Del: spam -- https://master.php.net/note/delete/103030/spam Del: non-english -- https://master.php.net/note/delete/103030/non-english Del: in docs -- https://master.php.net/note/delete/103030/in+docs Del: other reasons-- https://master.php.net/note/delete/103030 Reject -- https://master.php.net/note/reject/103030 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178058) next »