JavaScript to Standard Form converter (times 10^x)


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. function standardForm(s) {
  2. var indexOfE = s.indexOf('e');
  3. if(indexOfE != -1) {
  4. var s_start = s.substr(0,indexOfE) + ' × 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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.