note 47983 added to function.number-format

From: Date: Wed, 08 Dec 2004 04:21:40 +0000
Subject: note 47983 added to function.number-format
Groups: php.notes 
Request: Send a blank email to php-notes+get-81510@lists.php.net to get a copy of this message
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, '.', ''); ---- Manual Page -- http://www.php.net/manual/en/function.number-format.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47983 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47983&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47983&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47983&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47983&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47983&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47983&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47983&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81510) next »