EchoFool Posted January 24, 2010 Share Posted January 24, 2010 Hey I have a simple function which isn't giving me the correct answer yet when i echo what is going on the numbers are correct but the answer is not... this is what i got: <?php Echo $FreeSpace.' divided by '.$Size.' = '.number_format(floor($FreeSpace/$Size)).'<br>'; ?> And this is what resulted: 249,100 divided by 1 = 249 Clearly not correct lol what did i do wrong? Link to comment https://forums.phpfreaks.com/topic/189653-php-function-issue/ Share on other sites More sharing options...
PHP Monkeh Posted January 24, 2010 Share Posted January 24, 2010 The reason is the comma separator in your variable $FreeSpace. I don't know the specifics of why it's the case, my only guess is PHP sees it as a string rather than a numeric value (even though it does change the string "249,000" to 249). If you remove the comma from $FreeSpace you'll find that it works fine. Link to comment https://forums.phpfreaks.com/topic/189653-php-function-issue/#findComment-1000933 Share on other sites More sharing options...
EchoFool Posted January 24, 2010 Author Share Posted January 24, 2010 I see - how can i revert it back to an integer for the sum to work ? is there a function opposite to number_format? Link to comment https://forums.phpfreaks.com/topic/189653-php-function-issue/#findComment-1000937 Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2010 Share Posted January 25, 2010 You coud do an not-so-elegant str_replace() on commas in $FreeSpace I guess - it would be better though if you did this when you were initialising the $FreeSpace variable. Or if it's being pulled from a database, removing the comma before inserting the value to the DB. Link to comment https://forums.phpfreaks.com/topic/189653-php-function-issue/#findComment-1001184 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.