note 47983 deleted from function.number-format by alindeman
| From: | alindeman@php.net | Date: | Fri, 10 Dec 2004 00:08:53 +0000 |
| Subject: | note 47983 deleted from function.number-format by alindeman | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-81619@lists.php.net to get a copy of this message | ||
Note Submitter: Ramosa (at) gmail
----
This is a solution I came up with to convert an arbitrary price (you don't know what locale it
has) to standard english format.
It's pretty hard to know that danish 8,559 actually means 8559 and not 8 point 559 isn't
it?
We know that a price has 2 decimals for fraction.
// check if dot is used as fraction seperator
if ( (strlen($price)-strrpos($price, ".")) == 2 ) {
// in that case, comma would be thousands delimiter, rid it
$price = str_replace(',', '', $price);
}
// check if comma is used as fraction seperator
else if ( (strlen($price)-strrpos($price, ",")) == 2 ) {
// in that case, dot would be thousands delimiter, rid it
$price = str_replace('.', '', $price);
}
// ok we have no fraction, in that case get rid of dot and comma
else {
$price = str_replace('.', '', $price);
$price = str_replace(',', '', $price);
}
// and finally convert to regular english format
$price = number_format($price, 2, '.', '');