Scientific Notation Calculator
Three tools in one: convert standard form to scientific notation (hop counting included),
convert back to standard form, or multiply numbers in scientific notation step by step.
Convert to scientific notation
Enter any positive number. The calculator counts the hops and builds N × 10e.
Convert to standard form
Enter a coefficient and an exponent. The calculator hops the decimal the right way.
Multiply in scientific notation
Enter two numbers. The calculator multiplies coefficients, adds exponents, and renormalizes.
/* ---------------- pure logic (tested in node) ---------------- */ function normNumStr(s){ s = String(s).replace(/,/g, '').replace(/\s+/g, ''); var m = /^(\d*)(?:\.(\d*))?$/.exec(s); if (!m || !(m[1] !== '' || m[2])) return null; var ip = m[1].replace(/^0+/, ''), fp = (m[2] || '').replace(/0+$/, ''); if (ip === '') ip = '0'; return fp ? ip + '.' + fp : ip; } function stdToSci(s){ s = String(s).replace(/,/g, '').replace(/\s+/g, ''); var m = /^(\d*)(?:\.(\d*))?$/.exec(s); if (!m || !(m[1] !== '' || m[2])) return null; var ip = m[1].replace(/^0+/, ''), fp = m[2] || ''; if (ip !== ''){ var rest = (ip.slice(1) + fp).replace(/0+$/, ''); return { c:ip[0] + (rest ? '.' + rest : ''), e:ip.length - 1 }; } var k = 0; while (!(k >= fp.length || fp[k] !== '0')) k++; if (k === fp.length) return null; var rest2 = fp.slice(k + 1).replace(/0+$/, ''); return { c:fp[k] + (rest2 ? '.' + rest2 : ''), e:-(k + 1) }; } function sciToStd(c, e){ var parts = String(c).split('.'); var ipi = parts[0].replace(/^0+/, '') || '0'; var fp = parts[1] || ''; var digits = ipi + fp; var newPos = ipi.length + e; var out; if (newPos <= 0) out = '0.' + '0'.repeat(-newPos) + digits; else if (newPos >= digits.length) out = digits + '0'.repeat(newPos - digits.length); else out = digits.slice(0, newPos) + '.' + digits.slice(newPos); if (out.indexOf('.') !== -1) out = out.replace(/0+$/, '').replace(/\.$/, ''); out = out.replace(/^0+(?=\d)/, ''); if (out === '' || out[0] === '.') out = '0' + out; return out; } function mulDecStr(a, b){ var pa = (String(a).split('.')[1] || '').length; var pb = (String(b).split('.')[1] || '').length; var ia = parseInt(String(a).replace('.', ''), 10); var ib = parseInt(String(b).replace('.', ''), 10); var prod = ia * ib, places = pa + pb; var s = String(prod); while (s.length <= places) s = '0' + s; var res = places ? s.slice(0, s.length - places) + '.' + s.slice(s.length - places) : s; if (res.indexOf('.') !== -1) res = res.replace(/0+$/, '').replace(/\.$/, ''); return res.replace(/^0+(?=\d)/, '') || '0'; } function sciNormalize(c, e){ var parts = String(c).split('.'); var ipi = parts[0].replace(/^0+/, '') || '0'; var fp = (parts[1] || '').replace(/0+$/, ''); if (ipi.length > 1){ var shift = ipi.length - 1; var nc = ipi[0] + '.' + (ipi.slice(1) + fp).replace(/0+$/, ''); nc = nc.replace(/\.$/, ''); return { c:nc, e:e + shift }; } return { c:fp ? ipi + '.' + fp : ipi, e:e }; } function sciMultiply(c1, e1, c2, e2){ return sciNormalize(mulDecStr(c1, c2), e1 + e2); } function sciHTML(c, e){ return '' + c + ' × 10' + e + ''; } function escHTML(s){ return String(s).replace(/[&<>"']/g, function(ch){ return {'&':'&','<':'<','>':'>','"':'"',"'":'''}[ch]; }); } function fmtInt(s){ var parts = String(s).split('.'); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ','); return parts.join('.'); } function intOrNull(raw){ var s = String(raw).replace(/\s+/g, ''); if (!/^-?\d+$/.test(s)) return null; return parseInt(s, 10); } function toSciSteps(raw){ var s = stdToSci(raw); if (s === null) return { ok:false, err:'Enter a positive number (for example 32000 or 0.00045).' }; var hops = Math.abs(s.e); var steps = []; steps.push('Count the hops: move the decimal ' + hops + ' place' + (hops === 1 ? '' : 's') + (s.e > 0 ? ' LEFT (big number).' : s.e < 0 ? ' RIGHT (small number).' : ' — already proper.')); steps.push('Coefficient N = ' + s.c + ' (one nonzero digit before the point).'); steps.push('Exponent = ' + (s.e >= 0 ? '+' : '') + s.e + ' = the hop count with the sign.'); return { ok:true, steps:steps, ansHTML:sciHTML(s.c, s.e), ansText:s.c + '×10^' + s.e }; } function toStdSteps(cRaw, eRaw){ var cn = normNumStr(cRaw); var e = intOrNull(eRaw); if (cn === null || e === null) return { ok:false, err:'Enter a coefficient (like 3.2) and a whole-number exponent (like 4 or -3).' }; var v = sciToStd(cn, e); var steps = []; if (e >= 0) steps.push('Positive exponent: hop the decimal ' + e + ' places RIGHT — the number grows.'); else steps.push('Negative exponent: hop the decimal ' + (-e) + ' places LEFT past the front — the number shrinks, gaining leading zeros.'); steps.push('Answer: ' + fmtInt(v) + '.'); return { ok:true, steps:steps, ansHTML:'' + escHTML(fmtInt(v)) + '', ansText:v }; } function multiplySciSteps(c1r, e1r, c2r, e2r){ var c1 = normNumStr(c1r), c2 = normNumStr(c2r); var e1 = intOrNull(e1r), e2 = intOrNull(e2r); if (c1 === null || c2 === null || e1 === null || e2 === null) return { ok:false, err:'Enter two coefficients and two whole-number exponents.' }; var prod = mulDecStr(c1, c2); var m = sciMultiply(c1, e1, c2, e2); var steps = []; steps.push('Multiply coefficients: ' + c1 + ' × ' + c2 + ' = ' + prod + '.'); steps.push('Add exponents: ' + e1 + ' + ' + e2 + ' = ' + (e1 + e2) + ' → ' + prod + ' × 10' + (e1 + e2) + '.'); if (m.c === prod) steps.push('The coefficient is already proper (1 ≤ N < 10). Done.'); else steps.push('Renormalize: ' + prod + ' is not proper form — hop once → ' + m.c + ' × 10' + m.e + '.'); return { ok:true, steps:steps, ansHTML:sciHTML(m.c, m.e), ansText:m.c + '×10^' + m.e }; }
/* ---------------- UI wiring ---------------- */ function el(id){ return document.getElementById(id); } function renderCalc(res, errId, resId, ansId, stepsId){ var err = el(errId), res = el(resId); if (!res.ok){ err.textContent = res.err; err.classList.add('show'); res.classList.remove('show'); return; } err.classList.remove('show'); el(ansId).innerHTML = 'Answer: ' + res.ansHTML + ''; el(stepsId).innerHTML = res.steps.map(function(s){ return '
'; }).join(''); res.classList.add('show'); } function initTool(){ var tabs = document.querySelectorAll('.tabbtn'); for (var i = 0; i < tabs.length; i++){ tabs[i].addEventListener('click', function(){ for (var j = 0; j < tabs.length; j++) tabs[j].classList.remove('active'); this.classList.add('active'); var panels = document.querySelectorAll('.panel'); for (var k = 0; k < panels.length; k++) panels[k].classList.remove('active'); document.getElementById(this.getAttribute('data-tab')).classList.add('active'); }); } el('t1go').addEventListener('click', function(){ renderCalc(toSciSteps(el('t1n').value), 't1err', 't1res', 't1ans', 't1steps'); }); el('t2go').addEventListener('click', function(){ renderCalc(toStdSteps(el('t2c').value, el('t2e').value), 't2err', 't2res', 't2ans', 't2steps'); }); el('t3go').addEventListener('click', function(){ renderCalc(multiplySciSteps(el('t3c1').value, el('t3e1').value, el('t3c2').value, el('t3e2').value), 't3err', 't3res', 't3ans', 't3steps'); }); } if (!(typeof document === 'undefined' || !document.addEventListener)){ document.addEventListener('DOMContentLoaded', initTool); }