Jump to content

[SOLVED] Scientific Notation to Regular Integer


xProteuSx

Recommended Posts

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.