unemployment Posted September 27, 2011 Share Posted September 27, 2011 How can I make $10.58 round up to $11? Right now my code makes $10.58 into $1,058 if (preg_match_all('#([0-9]+)#', $_POST['capitalrequestedas'], $matches) > 0) { $capitalrequested = implode($matches[1]); } else { $capitalrequested = 0; } Quote Link to comment https://forums.phpfreaks.com/topic/247953-change-money-format/ Share on other sites More sharing options...
Psycho Posted September 27, 2011 Share Posted September 27, 2011 This really isn't the right scenario for preg_match(). Instead use preg_replace to remove all non-numeric characters (i.e. not digits or decimals). //Remove all non numbers/decimals $capitalrequested = preg_replace('#[^\d.]#', '', $_POST['capitalrequestedas']); //If a valid number, round to whole nuber, else set to 0 $capitalrequested = (is_numeric($capitalrequested)) ? round($capitalrequested) : 0; If the value can be negative, then the regular expression would need to be modified to retain the negative symbol. Quote Link to comment https://forums.phpfreaks.com/topic/247953-change-money-format/#findComment-1273237 Share on other sites More sharing options...
unemployment Posted September 27, 2011 Author Share Posted September 27, 2011 Thank you... That worked Quote Link to comment https://forums.phpfreaks.com/topic/247953-change-money-format/#findComment-1273251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.