Despite seeming like a very basic procedure, this evening I found I had wasted half an hour, split between Googling for the answer and trying to work it out for myself.
I have a number that needs to be stored in a string format in PHP. However, I need to use this value at some point to run a calculation, for which I need the true value as opposed to the string. If I had just wanted a whole number the answer is very simple, use the PHP (int) method to convert it.
It is not that easy when you are working with decimals. Unless I am missing something (in which case feel free to embarass me in the comments below!) there is no easy way to do this. Here is what I came up with:
$string = "3.142"; // The source number, as a string
$a = explode(".",$string); // Split the string, using the decimal point as separator
$int = $a[0]; // The section before the decimal point
$dec = $a[1]; // The section after the decimal point
$lengthofnum=strlen($dec); // Get the num of characters after the decimal point
$divider="1"; // This sets the divider at 1
$i=0;
while($i < ($lengthofnum)) {
$divider.="0"; // Adds a zero to the divider for every char after the decimal point
$i++;
}
$divider=(int)$divider; // Converts the divider (currently a string) to an integer
$total=$int+($dec/$divider); // compiles the total back as a numeric value
If anyone knows of a quicker / cleaner / better / more funky way of doing this simple task please do let me know… if not then I hope this little snippet at least lets you avoid losing the half our of your life that I lost this evening!
UPDATE: Thanks to Xobman, who Twittered me this little gem:
Try $foo = “3.123″; $bar = (float) $foo;
LOL… low and behold, it works! That’ll teach me to post my (oh so clever!) code on the Interweb for all to see
Thanks Xobman!
Stumble it!
March 19th, 2009 at 5:24 am
If you feel like inventing function that should be native – you usually are.
March 19th, 2009 at 2:59 pm
Any hour spent coding cannot be a waste, you always learn something from the experience. At least now we know that Twitter can indeed be very handy.
March 19th, 2009 at 4:42 pm
@Rarst + Lyndi: You’re right, these things are often a lot easier than they first seem. Also, Twitter does seem a great way of asking these questions. Just a shame in my case I asked the question AFTER I had spent the time
September 4th, 2009 at 8:54 am
You can use intval()
October 1st, 2010 at 3:42 pm
It’s still not so easy if the string cotains thousands separators or other such nonsense. For example, “$12,500.05″.
What I do is split at the decimal point as noted above ie: explode(‘.’,$string).
Next, I use count() to make sure that my array has no more than two entries, otherwise I error out because if there is more than 1 decimal point, there’s obviously big problems with the string.
Then I use ereg_replace(“[^[:digit:]]”,”",$arr[x]) to strip out all non-numeric characters from both array entries.
Finally I put it all back together and create a float, $number = (float) “$arr[0].$arr[1]“
November 12th, 2010 at 12:38 pm
Great solution Stan! With that approach it is pretty easy to deal with any thousand separator, either commas or spaces of whatever else.
December 22nd, 2010 at 8:44 am
Thanks a lot, Stan!
January 6th, 2011 at 5:15 am
http://www.php.net/manual/en/function.floatval.php
Did you try floatval() ?
January 7th, 2011 at 1:24 am
thanx Stan” would you put the code? i cant figure out how to do it yet.
February 25th, 2011 at 3:20 pm
Thanks so much! That little float thing worked a treat!
July 3rd, 2011 at 12:56 am
I need to go the other way around. I have a phone number I need to convert to a number. Like this: 555-555-5555 to 5555555555. How do I get rid of the dashes?
August 17th, 2011 at 10:28 am
Use str_replace.
I need to go the other way around. I have a phone number I need to convert to a number. Like this: 555-555-5555 to 5555555555. How do I get rid of the dashes?
August 17th, 2011 at 4:47 pm
Thanks for the response Jay. That’s what I did, and it works flawlessly.
September 2nd, 2011 at 7:13 pm
Thanks for your work! I was in the same boat but, thanks to you, my Googling paid off immediately!
September 4th, 2011 at 9:06 am
555-555-5555 to 5555555555:
$y = ’555-555-5555′;
$x = str_replace(‘-’, ”, $y);
October 12th, 2011 at 8:30 am
I have a string $a = ‘AT0001′
and I need it become ‘AT0002′ in the next entry automatically and become ‘AT0003′ , ‘AT0025′ and so on..
please could someone help me how to do it?
February 21st, 2012 at 6:18 am
i have int to convert into string.
March 26th, 2012 at 5:38 am
Please help me to convert the following string to give me the result 38.
“2*5+10*2+2*4″
April 27th, 2012 at 12:40 am
$val = 2*5+10*2+2*4;
the * / are accomplished first
then the + -
your operator is correct but you CANNOT USE QUOTES
September 26th, 2012 at 6:54 pm
Am I missing something or this just works fine:
echo gettype( (“12.123″ + 0) );
=> double
echo gettype( (“12″ + 0) );
=> integer
?