Skill: percentages
Percent Converter & Percent-of Calculator
Convert any form to the other two with steps shown, or compute a percent of a number.
Convert between fraction, decimal, and percent
Type any one form — a decimal like 0.75, a percent like 75%, or a fraction like 3/4 — and get all three with the working shown.
Percent of a number
Converts the percent to a decimal first — never multiplies by the raw percent number.
(function(){ 'use strict'; var btns = document.querySelectorAll('.tabbtn'); btns.forEach(function(b){ b.addEventListener('click', function(){ btns.forEach(function(x){ x.classList.remove('active'); }); document.querySelectorAll('.panel').forEach(function(p){ p.classList.remove('active'); }); b.classList.add('active'); document.getElementById(b.getAttribute('data-tab')).classList.add('active'); }); }); })();
// __PURE_BEGIN__ var TOOL_API = (function(){ 'use strict'; function gcd(a, b){ a = Math.abs(a); b = Math.abs(b); while (b){ var t = a % b; a = b; b = t; } return a || 1; } function fnum(v, p){ var neg = v < 0, s = String(Math.abs(Math.round(v))); while (s.length <= p) s = '0' + s; var ip = s.slice(0, s.length - p), fp = s.slice(s.length - p).replace(/0+$/, ''); return (neg ? '-' : '') + (fp ? ip + '.' + fp : ip); } function parseVal(str){ var s = String(str).trim(); var m = /^(-?\d+(?:\.\d+)?)\s*\/\s*(-?\d+(?:\.\d+)?)$/.exec(s); if (m){ var n = parseFloat(m[1]), d = parseFloat(m[2]); if (d === 0) return null; return {value:n / d, form:'fraction', n:n, d:d}; } var pm = /^(-?\d+(?:\.\d+)?)\s*%$/.exec(s); if (pm) return {value:parseFloat(pm[1]) / 100, form:'percent', p:parseFloat(pm[1])}; if (/^-?(\d+(\.\d+)?|\.\d+)$/.test(s)) return {value:parseFloat(s), form:'decimal'}; return null; } function convert(s){ var r = parseVal(s); if (!r) return {ok:false, error:'Enter a decimal (0.75), a percent (75%), or a fraction (3/4).'}; var v = r.value, steps = []; var pct = v * 100; var pctStr = String(Math.round(pct * 1e10) / 1e10); var decStr = String(Math.round(v * 1e10) / 1e10); /* fraction: use a denominator of 10000 and reduce (good for terminating inputs) */ var fn = Math.round(v * 10000), fd = 10000, g = gcd(fn, fd); fn /= g; fd /= g; var fracStr = fd === 1 ? String(fn) : fn + '/' + fd; if (r.form === 'fraction') steps.push(r.n + '/' + r.d + ' = ' + r.n + ' ÷ ' + r.d + ' = ' + decStr + '.'); if (r.form === 'percent') steps.push(r.p + '% means ' + r.p + ' per hundred: ' + r.p + ' ÷ 100 = ' + decStr + '.'); if (r.form === 'decimal') steps.push('Starting from the decimal ' + decStr + '.'); steps.push('Decimal → percent: slide the point two places right → ' + pctStr + '%.'); steps.push('Percent → fraction: ' + pctStr + '/100 simplifies to ' + fracStr + '.'); return {ok:true, decimal:decStr, percent:pctStr + '%', fraction:fracStr, steps:steps, value:v}; } function percentOf(pStr, nStr){ var p = parseFloat(String(pStr).trim()), n = parseFloat(String(nStr).trim()); if (!(p >= 0) || !(n >= 0) || String(pStr).trim() === '' || String(nStr).trim() === '') return {ok:false, error:'Enter a percent and a number, like 15 and 80.'}; var dec = p / 100, ans = dec * n; var ansStr = String(Math.round(ans * 1e10) / 1e10); return {ok:true, result:ansStr, steps:[ 'Convert the percent first: ' + p + '% = ' + p + ' ÷ 100 = ' + dec + '.', 'Multiply: ' + dec + ' × ' + n + ' = ' + ansStr + '.' ]}; } return {parseVal:parseVal, convert:convert, percentOf:percentOf, gcd:gcd}; })(); // __PURE_END__
(function(){ 'use strict'; function $(id){ return document.getElementById(id); } function esc(s){ return String(s).replace(/\x26/g,'&').replace(//g,'>'); } document.addEventListener('DOMContentLoaded', function(){ $('calcC').addEventListener('click', function(){ var r = TOOL_API.convert($('cv').value); var err = $('errC'), res = $('resC'); err.classList.remove('show'); res.classList.remove('show'); if (!r.ok){ err.textContent = r.error; err.classList.add('show'); return; } $('ansC').innerHTML = 'Decimal ' + esc(r.decimal) + ' · Percent ' + esc(r.percent) + ' · Fraction ' + esc(r.fraction) + ''; $('stepsC').innerHTML = r.steps.map(function(s){ return '
'; }).join(''); res.classList.add('show'); }); $('calcP').addEventListener('click', function(){ var r = TOOL_API.percentOf($('pp').value, $('pn').value); var err = $('errP'), res = $('resP'); err.classList.remove('show'); res.classList.remove('show'); if (!r.ok){ err.textContent = r.error; err.classList.add('show'); return; } $('ansP').innerHTML = 'Answer: ' + esc(r.result) + ''; $('stepsP').innerHTML = r.steps.map(function(s){ return '
'; }).join(''); res.classList.add('show'); }); }); })();