Convert num from scientific notation to float

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Convert num from scientific notation to float

Post by Luke »

How would I convert a number from 0.420049480000000E+02 format to a float or double?
Last edited by Luke on Thu Jul 13, 2006 1:36 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Convert num from 0.420049480000000E+02 to float

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

no, (float) didn't do it... made it 0... and settype turned it into a 1. Any other suggestions?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

substr($string,1,-1)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

nevermind I got it :D

Code: Select all

trim($input, '"')
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

thanks man... I appreciate that. seriously. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I DID IT!!!!
Eh... and what does it do? Drawing curves on google maps?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Weirdan wrote:
I DID IT!!!!
Eh... and what does it do? Drawing curves on google maps?
Outlines counties based on coordinate lists.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Outlines counties based on coordinate lists.
Oh wow that is impressive. Not that hard though, I imagine.
Post Reply