0

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?

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
Mauro
  • 361
  • 1
  • 4
  • 14
  • Use a regular expression to get the parts of the number. Then use `number_format` to print the mantissa with 2 decimal places. – Barmar Aug 16 '14 at 14:49
  • Mind you, `2.95^-10` is something entirely different from `2.95e-10` in my book. It would confuse me as a reader in scientific texts (after all, 2e3 => 2000, 2^2 => 2*2*2 = 8). – Wrikken Aug 16 '14 at 15:01
  • you are absolutely right! my fault.. thanks for this fix, you made my day – Mauro Aug 16 '14 at 17:15

2 Answers2

2

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!

Mauro
  • 361
  • 1
  • 4
  • 14
0

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>";
Wrikken
  • 69,272
  • 8
  • 97
  • 136