Converting a PHP string number into a numeric value

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!

It’s official, I am a supermarket!

Ok, not quite, but in the world of the interweb it seems I have set up shop as one of the largest supermarkets in the UK.

Last week I noticed a spike in traffic to one of my sites. When I checked the stats I could see the traffic was coming from people searching for a particular domain name in Google. People often seem to type web URL’s into the Google search field, so it is worth making sure you rank high for your domain name too!

For some reason when people entered the URL of this supermarket into Google my site ranked number 1. I found this to be quite amusing.

What I found even funnier is once they clicked my link people seemed to think I WAS the supermarket. It turns out the supermarket had a prize draw and to enter you had to fill out a survey. People were registering on my site and emailing me their opinions thinking they were in the running for a prize.

This is most amusing, as my site looks nothing like the supermarkets branding, it isn’t even close.

I do find it odd that the proper site didn’t even rank in the top 10 on Google though. They must really be in the Google bad-books!

I pointed these people in the right direction, it seemed the right thing to do, but whoever the supermarket employ for their SEO probably needs a lesson in Googlisation!