Created
July 22, 2011 17:03
-
-
Save jonathonbyrdziak/1099866 to your computer and use it in GitHub Desktop.
Working with currency and string to integers in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function is responsible for stripping anything out of a string value that is meant to | |
* be an currency amount or other float. This function then converts the numbers left | |
* into a float value and formatting it appropriately. | |
* | |
* @param string $string | Can be anything but mostly for $23.23 | |
* @return float $string | returns 23.23 as a valid integer that can be used in math. | |
*/ | |
function _mint( $string ) | |
{ | |
$string = str_replace(array('$','$'), '', $string); | |
$string = trim(preg_replace("([^0-9.])", "", $string)); | |
$string = number_format(floatval($string), 2, '.', ','); | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment