Page 1 of 2

Convert num from scientific notation to float

Posted: Thu Jul 13, 2006 11:43 am
by Luke
How would I convert a number from 0.420049480000000E+02 format to a float or double?

Posted: Thu Jul 13, 2006 12:13 pm
by pickle
Break it up & do some math. I'm kind of guessing here, but doesn't that number = 42.004948 or does it =0.0042004948 or does it = something else my feeble intellect can't comprehend?

Posted: Thu Jul 13, 2006 12:15 pm
by Luke
pickle wrote:42.004948
I believe this is correct. I was just wondering if there was a built-in function... the math isn't difficult, but I figured why reinvent the wheel? I'll just do the math I guess. Thanks man.

Re: Convert num from 0.420049480000000E+02 to float

Posted: Thu Jul 13, 2006 12:49 pm
by Ollie Saunders
The Ninja Space Goat wrote:How would I convert a number from 0.420049480000000E+02 format to a float or double?
You might want to test but even for dodgy string like 0.420049480000000E+02 i think this should work:

Code: Select all

$var = '0.420049480000000E+02';
$floatVar = (float)$var;
PHP definately supports scientific notation.

Posted: Thu Jul 13, 2006 1:35 pm
by Luke
no, (float) didn't do it... made it 0... and settype turned it into a 1. Any other suggestions?

Posted: Thu Jul 13, 2006 1:44 pm
by Weirdan
The Ninja Space Goat wrote:no, (float) didn't do it... made it 0... and settype turned it into a 1. Any other suggestions?
did it for me:

Code: Select all

$var = '0.420049480000000E+02';
    $floatVar = (float)$var;
    var_dump(phpversion());
    var_dump($floatVar);

// string(5) "5.1.2"
// float(42.004948)


// string(5) "4.4.2"
// float(42.004948)

Posted: Thu Jul 13, 2006 1:47 pm
by Luke
maybe it's because mine is stored in the database with quotes around it like "-0.122525406135743E+03"

This sounds really stupid, but I didn't get much sleep last night... how do I get rid of those quotes?

Posted: Thu Jul 13, 2006 1:50 pm
by Ollie Saunders
substr($string,1,-1)

Posted: Thu Jul 13, 2006 1:51 pm
by Luke
nevermind I got it :D

Code: Select all

trim($input, '"')

Posted: Thu Jul 13, 2006 1:57 pm
by Luke
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D

I DID IT!!!!

http://www.mc2design.com/google/

EDIT:
:cry: :cry: Doesn't work in IE... back to the drawing board... sigh

Posted: Thu Jul 13, 2006 2:11 pm
by Roja
The Ninja Space Goat wrote:I DID IT!!!!

http://www.mc2design.com/google/
*SERIOUSLY* impressive. VERY cool.

So cool, *I* didn't even look at the viewsource/validation! Seriously, very cool work. Congrats!

Posted: Thu Jul 13, 2006 2:15 pm
by Luke
thanks man... I appreciate that. seriously. :)

Posted: Thu Jul 13, 2006 2:27 pm
by Weirdan
I DID IT!!!!
Eh... and what does it do? Drawing curves on google maps?

Posted: Thu Jul 13, 2006 2:31 pm
by Roja
Weirdan wrote:
I DID IT!!!!
Eh... and what does it do? Drawing curves on google maps?
Outlines counties based on coordinate lists.

Posted: Thu Jul 13, 2006 2:52 pm
by Ollie Saunders
Outlines counties based on coordinate lists.
Oh wow that is impressive. Not that hard though, I imagine.