Return to Snippet

Revision: 51237
at September 18, 2011 22:46 by Jellybean732


Initial Code
function standardForm(s) {
	var indexOfE = s.indexOf('e');
	if(indexOfE != -1) {
		var s_start = s.substr(0,indexOfE) + ' × 10';
		var s_index = (s.charAt(indexOfE+1) == '+') ? s.substring(indexOfE+2,s.length) : s.substring(indexOfE+1,s.length);
		var s_end = '<sup>'+s_index+'</sup>';
		return s_start+s_end;
	} else {
		return s;
	}
}

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

Initial Description
Use this to convert JavaScript numbers to official scientific standard form (e.g. 1.54 &times; 10^3)
Uses HTML &lt;sup&gt; tags and &times; symbol

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

Initial Title
JavaScript to Standard Form converter (times 10^x)

Initial Tags
form

Initial Language
JavaScript