ballhogjoni Posted September 21, 2007 Share Posted September 21, 2007 Does anyone know of a decimal function. Basically I want to have the script replace the decimal point as follows: someone inputs 10% into a field and I want the script to place a decimal point like this, .10 so I can do some math with it. Quote Link to comment https://forums.phpfreaks.com/topic/70207-solved-decimal-function/ Share on other sites More sharing options...
recklessgeneral Posted September 21, 2007 Share Posted September 21, 2007 Hi, The following code will take strip the percent sign from the given string, remove any whitespace and cast the value to an integer. Here, I divide by 100 when printing out, but you can store it in another variable or whatever. <?php $percent = "15%"; $num = (int) trim(str_replace ('%', '', $percent)); echo "Percentage is " . ($num / 100); ?> You'd probably want to verify that the variable passed in to str_replace was of the correct format, probably by using regular expressions or something. You may want to change the cast from an int to a float, if that's valid input. Cheers, Darren. Quote Link to comment https://forums.phpfreaks.com/topic/70207-solved-decimal-function/#findComment-352633 Share on other sites More sharing options...
Barand Posted September 21, 2007 Share Posted September 21, 2007 As the numeric value of a string is the value of any numeric digits prior to the first non-numeric character, then <?php $percent = "15%"; echo $percent/100; // 0.15 ?> Quote Link to comment https://forums.phpfreaks.com/topic/70207-solved-decimal-function/#findComment-352649 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.