REMOVING DOLLAR SIGNS FROM NUMERICAL VALUES - Oct 26th, 2012


The easiest way to remove dollar signs from your data is not to allow them in the first place. But if you have to
include them, Greg Thomas, a developer at Interactive Tools offered this advice.

If you want to strip the dollar sign and comma from a number you could use these functions together:



$value = preg_replace('/[\$,]/', '', $value);
$value = floatval($value);



The preg_replace function will strip out the dollar sign and any commas. The floatval function will convert the number
from a string to a floating value, ensuring it will behave like a number if you want to use it for addition,
multiplication, etc.

To convert the number back to a currency format I would use this:



echo '$ '.number_format($number, 2, '.', ',');



The number_format function will take a number and convert it into string format it. In this case it makes the number
have two decimal places and adds a comma for every 3 digits.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service