Posted By

Jellybean732 on 09/18/11


Tagged

form standard 10 x power e times scientific


Versions (?)

JavaScript to Standard Form converter (times 10^x)


 / Published in: JavaScript
 

URL: http://astrophysicsonline.info/resources/calculator.php

Use this to convert JavaScript numbers to official scientific standard form (e.g. 1.54 × 10^3) Uses HTML <sup> tags and × symbol

Example: var number = 4.223e20; var stnd = standardForm(number); // stnd = 4.223 x 10^20

  1. function standardForm(s) {
  2. var indexOfE = s.indexOf('e');
  3. if(indexOfE != -1) {
  4. var s_start = s.substr(0,indexOfE) + ' &times; 10';
  5. var s_index = (s.charAt(indexOfE+1) == '+') ? s.substring(indexOfE+2,s.length) : s.substring(indexOfE+1,s.length);
  6. var s_end = '<sup>'+s_index+'</sup>';
  7. return s_start+s_end;
  8. } else {
  9. return s;
  10. }
  11. }

Report this snippet  

You need to login to post a comment.