Mauro
Mauro

Reputation: 361

Simplify scientific number format and change formatting

Is there a tricky way to print in a "round" way the scientific numbers such as:

2.9560343062229E-10

let be converted to 2.95^10. Also, how to split the exponent in order to print 2.95-10 in HTML notation?

Upvotes: 0

Views: 78

Answers (2)

Wrikken
Wrikken

Reputation: 70460

Simple rounding to 2 in scientific notation:

$string = sprintf("%.2e",$number);

Custom format:

$exp = floor(log10($number));
$string = round($number/pow(10,$exp),2)."<sup>".$exp."</sup>";

Upvotes: 0

Mauro
Mauro

Reputation: 361

Ok, I'm done :P Thanks for your suggestions. Quckest way:

$str = explode("E", $str);
return round($str[0],2)."<sup>".$str[1]."</sup>";

thanks!

Upvotes: 2

Related Questions