note 99285 added to function.number-format
| From: | CodyG dot at osu1 dot php dot net | Date: | Sun, 08 Aug 2010 21:06:29 +0000 |
| Subject: | note 99285 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171473@lists.php.net to get a copy of this message | ||
Sometimes, I'm sure, you guys are looking for a way to do the exact opposite of this function:
convert a comma-filled number back to normal decimal.
Here's a quick code that popped into my head today:
<?php
$mynumstr = "100,000,000.75";
$mynum = doubleval(str_replace(",","",$mynumstr));
?>
This is especially useful when validating user input. Some people like to include comma's with
their numbers.
Some also like to include the dollar-sign when entering prices. Here's a modification:
<?php
$mynumstr = '$100,000,000.25';
$arr1 = array(',','$');
$arr2 = array('','');
$mynum = doubleval(str_replace($arr1,$arr2,$mynumstr));
//The arrays had to be defined in a
// variable to shorten statement.
?>
Okay, that takes care of it, I use the PHP4 array compatibility update to remove several characters.
Don't forget to either use single-quotes or escape the dollar-sign!
~Cody G.
----
Server IP: 69.147.83.197
Probable Submitter: 76.235.223.16
----
Manual Page -- http://www.php.net/manual/en/function.number-format.php
Edit -- https://master.php.net/note/edit/99285
Del: integrated -- https://master.php.net/note/delete/99285/integrated
Del: useless -- https://master.php.net/note/delete/99285/useless
Del: bad code -- https://master.php.net/note/delete/99285/bad+code
Del: spam -- https://master.php.net/note/delete/99285/spam
Del: non-english -- https://master.php.net/note/delete/99285/non-english
Del: in docs -- https://master.php.net/note/delete/99285/in+docs
Del: other reasons-- https://master.php.net/note/delete/99285
Reject -- https://master.php.net/note/reject/99285
Search -- https://master.php.net/manage/user-notes.php