note 99285 deleted from function.number-format by cmb
| From: | cmb@php.net | Date: | Sun, 08 Sep 2019 10:14:17 +0000 |
| Subject: | note 99285 deleted from function.number-format by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212121@lists.php.net to get a copy of this message | ||
Note Submitter: Cody G.
----
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.