Math function problem

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

Post Reply
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Math function problem

Post by S_henry »

I want to display 12345.6789 as 1.23456789e+4. Anybody knows what function in PHP I should use?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm not sure how to do scientific notation but I'm sure it's easily found on google.
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

Anybody has any idea pls? I just try to find it but it's really very hard to found in the internet (maybe for me).
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

I'm sure you can write a function based on the algorithm.

a.) Move the decimal point to the right of the first nonzero digit. Count the number of places you moved the decimal point.

b.) Multiply the number from step a.) times 10 raised to + or - the number of places you moved the decimal point.
Use + if you moved the decimal to the left.
Use - if you moved the decimal to the right.

Code: Select all

function sci($x, $d=-1) {
   $min=($x<0)?"-":"";
   $x=abs($x); 
   $e=floor(($x!=0)?log10($x):0);
   $x*=pow(10,-$e);
   $fmt=($d>=0)?".".$d:"";
   $e=($e>=0)?"+".sprintf("%02d",$e):"-".sprintf("%02d",-$e);
   return sprintf("$min%".$fmt."fe%s",$x,$e);
&#125;
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

Thanx. Your function is really great.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

sprintf("%.9e",1234.56789);
prints 1.23456789e+3
works in 4.3.4 at least...

edit: slight tweak to the format...
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

feyd wrote:

Code: Select all

sprintf("%.9e",1234.56789);
prints 1.23456789e+3
That doesn't print anything for me ..
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

same here... You sure that works feyd??
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

feyd wrote:

Code: Select all

sprintf("%.9e",1234.56789);
prints 1.23456789e+3
works in 4.3.4 at least...

edit: slight tweak to the format...
If your slight tweak was changing printf to sprintf, then you shouldn't have done that. It is wrong. Take the 's' off and it'll work fine.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Pyrite, how the hell did you pluck that code out of the air. It looks totally crazy.

Well done all the same 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php.net ;)
Post Reply