homepage Welcome to WebmasterWorld Guest from 54.227.34.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Round numbers?
ktsirig

5+ Year Member



 
Msg#: 3999581 posted 1:08 am on Oct 2, 2009 (gmt 0)

Hello, I have a list of numbers that can be in format like:


8.8458e-119
1.06542e-52
2.68e-36
2.91405e-35
0.0190644
0.0205511
0.004

Is there something you can suggest me that I use to have some more "elegance" appearance, i.e:

8.84e-119
1.06e-52
2.68e-36
2.91e-35
0.019
0.02
0.004

? Note that I have all these number formats and I want to use a command that could apply to all of them.

 

IanKelley

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3999581 posted 7:51 pm on Oct 2, 2009 (gmt 0)

There is no single function that will accomplish what you want as you are rounding to varied precisions. You would need to use the round() function in different ways.

For the scientific notation numbers, one solution would be to temporarily strip off the notation, and then round each number to two decimal places before popping the notation back on:

Non notation numbers it looks like you're rounding to a precision of 3 decimal places?

So something like this should do essentially what you want:

if (strpos($num,'e') !== false) {
list($num,$extra) = explode('e',$num);
$num = round($num,2).'e'.$extra;
}
else $num = round($num,3);

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved