note 94628 added to numberformatter.create

From: Date: Sun, 15 Nov 2009 16:26:18 +0000
Subject: note 94628 added to numberformatter.create
Groups: php.notes 
Request: Send a blank email to php-notes+get-162935@lists.php.net to get a copy of this message
When formatting durations using the NumberFormatter::DURATION type, you may also need to use NumberFormatter::setTextAttribute to get the desired output. <?php $fmt = new NumberFormatter('en', NumberFormatter::DURATION); // Outputs: string(7) "3:25:45" var_dump($fmt->format(12345)); // "%in-numerals" is the default ruleset, so this results in the same as above. $fmt->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%in-numerals"); // Outputs: string(7) "3:25:45" var_dump($fmt->format(12345)); $fmt->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%with-words"); // Outputs: string(31) "3 hours, 25 minutes, 45 seconds" var_dump($fmt->format(12345)); $fmt2 = new NumberFormatter('fr', NumberFormatter::DURATION); // Outputs: string(7) "12 345" // See notes below. var_dump($fmt2->format(12345)); ?> This is a little counter-intuitive because there is not much doc available about the DURATION type. Also, as far as I can tell, only the English (en) locale has support for the "%in-numerals" & "%with-words" rulesets. Other locales seem to simply format the input as if the DECIMAL type had been used (at least using "fr" or "de" as the target locale). One way to provide that feature across different locales is to extract the ruleset implicitely used by NumberFormatter::DURATION and adapt it for the locales you're targetting. Use NumberFormatter::getPattern to extract the ruleset. ---- Server IP: 89.31.146.176 Probable Submitter: 78.232.246.115 ---- Manual Page -- http://www.php.net/manual/en/numberformatter.create.php Edit -- https://master.php.net/note/edit/94628 Del: integrated -- https://master.php.net/note/delete/94628/integrated Del: useless -- https://master.php.net/note/delete/94628/useless Del: bad code -- https://master.php.net/note/delete/94628/bad+code Del: spam -- https://master.php.net/note/delete/94628/spam Del: non-english -- https://master.php.net/note/delete/94628/non-english Del: in docs -- https://master.php.net/note/delete/94628/in+docs Del: other reasons-- https://master.php.net/note/delete/94628 Reject -- https://master.php.net/note/reject/94628 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162935) next »