xProteuSx Posted December 28, 2008 Share Posted December 28, 2008 One of the columns in my database takes large integers. When they are input MySQL or PHP condenses the numbers to scientific notation, so if I output the field I get output such as: '1e+06'. Is there a PHP command to revert the number so that instead of 1e+06 I get 1000000?? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/ Share on other sites More sharing options...
DarkSuperHero Posted December 28, 2008 Share Posted December 28, 2008 try the following; <?php $n = 4.39518e+7; //YOUR NUMBER printf("SCINO = '%e'\n", $n); //%e specifies sci-notation printf("FLOAT = '%d'\n", $n); // %d specifies decimal ?> http://us2.php.net/sprintf Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724794 Share on other sites More sharing options...
xProteuSx Posted December 28, 2008 Author Share Posted December 28, 2008 Sorry, I should have mentioned that not all number are in scientific notation. If I use the method you have outlined, it screws up all non-scientific notation numbers. Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724797 Share on other sites More sharing options...
DarkSuperHero Posted December 28, 2008 Share Posted December 28, 2008 <?php $n = (int)4.39518e+7; //YOUR NUMBER to start $m = (float)4.39518e+7; echo $m; echo $n; /* returns the following on script ran by Zend Studio IDE. 43951800 43951800 */ ?> PHP Hard Type Variables...not very common, yet.... :-P Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724799 Share on other sites More sharing options...
xProteuSx Posted December 28, 2008 Author Share Posted December 28, 2008 $row[0] = 1e+06; $formattednumber = (int) $row[0]; echo $formattednumber; Output: '1' Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724806 Share on other sites More sharing options...
Mchl Posted December 28, 2008 Share Posted December 28, 2008 PHP Hard Type Variables...not very common, yet.... :-P No wonder, since no such thing exists. $row[0] = 1e+06; $formattednumber = (int) $row[0]; echo $formattednumber; Output: '1' Returns 1000000 here... Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724813 Share on other sites More sharing options...
DarkSuperHero Posted December 28, 2008 Share Posted December 28, 2008 PHP Hard Type Variables...not very common, yet.... :-P No wonder, since no such thing exists. $row[0] = 1e+06; $formattednumber = (int) $row[0]; echo $formattednumber; Output: '1' Returns 1000000 here... hahahhaha oops, isn't there a name for it ? i might be getting names for this confused... :-P Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724916 Share on other sites More sharing options...
Mchl Posted December 28, 2008 Share Posted December 28, 2008 It's called type casting and it will not "lock" the variable's type Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724938 Share on other sites More sharing options...
xProteuSx Posted December 28, 2008 Author Share Posted December 28, 2008 I still cannot get this to work. You mentioned that $row[0] = 1e+06; $formattednumber = (int) $row[0]; echo $formattednumber; Returns '1000000'!?!?!? Why does it not do this for me? the output I get is '1'. Please help! This is a vital part of my project! Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-724988 Share on other sites More sharing options...
Mchl Posted December 28, 2008 Share Posted December 28, 2008 Is this all of your code? Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-725005 Share on other sites More sharing options...
xProteuSx Posted December 29, 2008 Author Share Posted December 29, 2008 Well, its just a snippet obviously. I am fetching a record from a database. The value comes from the fifth column, and is assigned to the variable $row[4]. If I echo the variable $row[4] the output I get is: 1e+06 (which is exactly the same way that it is stored in the database, in a float type field; I have a sneaky suspicion that this is the problem). I am trying to expand the number to its original form: 1000000. I could up all the code, but there's a lot of crap to sift through. Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-725115 Share on other sites More sharing options...
xProteuSx Posted December 29, 2008 Author Share Posted December 29, 2008 Changed the MySQL field from 'float' to 'bigint' and everything worked out after that. Too bad we didn't actually figure out how to convert abbreviated floats to ints. Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-725141 Share on other sites More sharing options...
corbin Posted December 29, 2008 Share Posted December 29, 2008 It would come back as a string from MySQL, and when ever PHP converts a string to an int, it only takes up until the first non-numeric character. C:\Users\Corbin>php -r "echo (int) '1e+06';" 1 C:\Users\Corbin>php -r "echo (int) '2e+06';" 2 C:\Users\Corbin>php -r "echo (int) '3e+06';" 3 Converting it to a float and then an int should work though: C:\Users\Corbin>php -r "echo (int) ((float) '1e+06');" 1000000 C:\Users\Corbin>php -r "echo (int) ((float) '2e+06');" 2000000 C:\Users\Corbin>php -r "echo (int) ((float) '3e+06');" 3000000 Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-725155 Share on other sites More sharing options...
Mchl Posted December 29, 2008 Share Posted December 29, 2008 Oh my... One never can be sure what happens with this loose typing thing... (Example from php.net comments) <?php for( $tmp = 0, $i = 0; $i < 100; $i++ ) { $tmp += 100000; echo round($tmp),"\n"; } ?> freaks me out... Anyway: Corbin's answer still doesn't answer why $row[0] = 1e+06; $formattednumber = (int) $row[0]; echo $formattednumber; would return 1... unless this was not the actual code. Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-725195 Share on other sites More sharing options...
corbin Posted December 29, 2008 Share Posted December 29, 2008 My theory is that the 1e+06 came from the DB and was a string in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-725199 Share on other sites More sharing options...
Mchl Posted December 29, 2008 Share Posted December 29, 2008 That makes sense unless this was not the actual code. Quote Link to comment https://forums.phpfreaks.com/topic/138617-solved-scientific-notation-to-regular-integer/#findComment-725205 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.