Percentage difference
HTML for this example
Select the code and use your copy shortcut. Changes in this preview are temporary.
<!doctype html>
<html lang="en-US"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Percentage difference</title><style>*{box-sizing:border-box}body{margin:0;background:#f3f4f2;color:#202c32;font:16px/1.5 system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}main{max-width:1120px;margin:auto;padding:30px 28px 60px}header{margin-bottom:22px}.eyebrow{font-size:12px;letter-spacing:.14em;text-transform:uppercase;font-weight:750;color:#59676b}h1{font-size:clamp(25px,4vw,36px);line-height:1.15;letter-spacing:-.035em;margin:7px 0 10px}h2{font-size:21px;line-height:1.3;margin:0 0 16px}h3{font-size:17px;margin:0 0 10px}p{margin:8px 0 16px}label{display:block;font-size:14px;font-weight:650;margin:12px 0 5px}input,select,textarea,button{font:inherit}input,select,textarea{width:100%;min-width:0;border:1px solid #b6c0bf;border-radius:7px;background:#fff;color:#202c32;padding:10px 12px}textarea{resize:vertical;min-height:88px}input[type=checkbox]{width:18px;height:18px;vertical-align:middle;margin-right:8px}button{border:1px solid #b8c3c1;border-radius:7px;padding:10px 15px;background:#fff;color:#203632;font-weight:650;cursor:pointer}button.primary{background:#243f3c;color:white;border-color:#243f3c}button:hover{filter:brightness(.95)}button:disabled{opacity:.48;cursor:not-allowed}:focus-visible{outline:3px solid #b05814;outline-offset:3px}.buttons{display:flex;gap:8px;flex-wrap:wrap;margin-top:18px}.panel{background:#fff;border:1px solid #d7ddda;border-radius:12px;padding:24px}.split{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.1fr);gap:22px}.row{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:16px}.result{background:#e4eee8;border:1px solid #c5d6ca;border-radius:12px;padding:28px}.big{font-size:clamp(32px,5vw,56px);font-weight:740;line-height:1.14;letter-spacing:-.04em;overflow-wrap:anywhere;font-variant-numeric:tabular-nums}.muted{color:#59686a;font-size:14px}.tag{display:inline-block;background:#edf0ed;border-radius:4px;padding:3px 8px;font-size:12px;font-weight:700}.error{color:#9d301b;font-weight:650;min-height:24px;margin-top:10px}.note{border-left:3px solid #9a794a;padding:10px 15px;background:#faf5e9;margin:18px 0}.below{margin-top:28px}.record{white-space:pre-wrap;overflow-wrap:anywhere;font:14px/1.65 ui-monospace,SFMono-Regular,Consolas,monospace;background:#f2f4f1;padding:18px;border-radius:8px}table{width:100%;border-collapse:collapse;font-size:14px}th,td{text-align:left;vertical-align:top;border-bottom:1px solid #dce1dd;padding:12px 10px;overflow-wrap:anywhere}th{color:#59686a;font-size:12px}a{color:#2c5a6a;overflow-wrap:anywhere}details{margin-top:20px}summary{cursor:pointer;font-weight:650}hr{border:0;border-top:1px solid #d6dcd6;margin:22px 0}[hidden]{display:none!important}ol,ul{padding-left:23px}.statline{display:flex;gap:20px;flex-wrap:wrap;margin:18px 0}.statline strong{display:block;font-size:24px}.inline{display:flex;align-items:center;gap:8px}.inline label{margin:0} .blank{min-height:36px;border-bottom:1px dashed #aebbb5;color:#66736d;font-size:13px;padding:8px 0}@media(max-width:720px){main{padding:22px 18px 45px}.split{grid-template-columns:1fr}.panel,.result{padding:20px}.row{gap:12px}th,td{padding:10px 6px}}@media(max-width:400px){main{padding:18px 14px 40px}.big{font-size:34px}button{padding:10px 12px}.row{grid-template-columns:1fr}.panel,.result{padding:17px}}</style></head><body><main><header><div class="eyebrow">Two proposals · One shared denominator</div><h1>Percentage difference</h1></header>
<section class="split"><div class="result" aria-live="polite"><span class="eyebrow">Symmetric difference</span><div id="answer" class="big">22.222222%</div><p class="muted">Compare two durations for the same task, with no baseline.</p><div class="statline"><div>Absolute gap<strong id="gap">10 minutes</strong></div><div>Shared mean<strong id="mean">45 minutes</strong></div></div><div id="formula" class="record">10 ÷ 45 × 100 = 200/9% ≈ 22.222222%</div><p class="muted">Ratio is dimensionless; display rounded to six decimal places.</p></div>
<form id="form" class="panel" novalidate><div class="row"><div><label for="a">Proposal A</label><input id="a" value="40" inputmode="decimal" aria-describedby="aError"><div id="aError" class="error"></div></div><div><label for="b">Proposal B</label><input id="b" value="50" inputmode="decimal" aria-describedby="bError"><div id="bError" class="error"></div></div></div><label for="unit">Common unit</label><input id="unit" value="minutes" maxlength="40"><label for="basis"><input type="checkbox" id="basis" checked>Same task and unit; neither value is Celsius</label><div class="buttons"><button class="primary" type="submit">Compare</button><button id="swap" type="button">Swap A and B</button><button id="reset" type="button">Reset</button></div><p id="feedback" class="error" role="status"></p></form></section>
<section class="below panel"><h2>Choose the denominator for the question</h2><table><thead><tr><th>Question</th><th>For 40 and 50</th></tr></thead><tbody><tr><td>Two unordered proposals</td><td>Gap 10 ÷ mean 45 ≈ 22.222222%</td></tr><tr><td>Change from 40 to 50</td><td>10 ÷ baseline 40 = 25% increase</td></tr><tr><td>Rates of 40% and 50%</td><td>10 percentage points</td></tr></tbody></table><p class="muted">Convert mismatched units separately. This comparison supplies no quality or improvement threshold. Both zero gives an undefined ratio.</p><a href="https://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php">Formula and symmetric comparison reference</a></section><footer class="below muted">Local working example · Changes last only while this page is open.</footer></main><script>
const $=id=>document.getElementById(id);const txt=(id,v)=>$(id).textContent=v;const esc=s=>String(s).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));const on=(id,event,fn)=>$(id).addEventListener(event,fn);const num=(s,positive=false)=>{s=String(s).trim();if(!/^\d+(?:\.\d{1,6})?$/.test(s)||!Number.isFinite(+s)||+s>1000000||(positive&&+s<=0))throw Error(positive?'Enter a density greater than 0, up to 1,000,000, with at most six decimals.':'Enter a value from 0 to 1,000,000, with at most six decimals.');return +s};const fmt=n=>n!==0&&Math.abs(n)<.000001?n.toExponential(6):Number(n.toFixed(6)).toString();
function difference(a,b){const gap=Math.abs(a-b),mean=(a+b)/2;return {gap,mean,percent:mean?gap/mean*100:null}}
function clear(){['answer','gap','mean','formula'].forEach(id=>txt(id,'—'));txt('feedback','Inputs changed. Compare to update.');txt('aError','');txt('bError','')}
function calculate(){clear();let a,b;for(const id of ['a','b'])try{const v=num($(id).value);if(id==='a')a=v;else b=v}catch(e){txt(id+'Error',e.message);txt('feedback','Correct the marked input.');return}if(!$('basis').checked||!$('unit').value.trim()){txt('feedback','Confirm the common unit and quantity basis.');return}const r=difference(a,b),u=$('unit').value.trim();txt('gap',fmt(r.gap)+' '+u);txt('mean',fmt(r.mean)+' '+u);txt('answer',r.percent===null?'Undefined':r.percent.toFixed(6)+'%');let exact='';const scale=1000000,ai=BigInt(Math.round(a*scale)),bi=BigInt(Math.round(b*scale));if(ai+bi){let n=(ai>bi?ai-bi:bi-ai)*200n,d=ai+bi;const gcd=(a,b)=>b?gcd(b,a%b):a;const g=gcd(n,d);exact=(n/g).toString()+(d/g===1n?'':'/'+(d/g))+'%';}txt('formula',r.percent===null?'0 ÷ 0: the mean is zero.':fmt(r.gap)+' ÷ '+fmt(r.mean)+' × 100 = '+exact+' ≈ '+r.percent.toFixed(6)+'%');txt('feedback',r.percent===null?'No ratio is defined when both values are zero.':'Comparison updated. Swapping preserves the ratio.')}
on('form','submit',e=>{e.preventDefault();calculate()});['a','b','unit','basis'].forEach(id=>on(id,'input',clear));on('swap','click',()=>{const a=$('a').value;$('a').value=$('b').value;$('b').value=a;calculate()});on('reset','click',()=>{$('a').value='';$('b').value='';$('unit').value='';$('basis').checked=false;clear();txt('feedback','');$('a').focus()});window.app={difference,calculate,clear};calculate();
</script>
<script>
// Local calculation controls: no native form submission or sandbox permissions required.
(()=>{const form=document.getElementById('form');if(!form)throw Error('Calculation form missing');const dispatch=()=>form.dispatchEvent(new Event('submit',{bubbles:true,cancelable:true}));const buttons=[...form.querySelectorAll('button')].filter(button=>button.type==='submit');if(buttons.length!==1)throw Error('Expected one calculation control');for(const button of buttons){button.type='button';button.addEventListener('click',dispatch);}form.addEventListener('keydown',event=>{if(event.key==='Enter'&&!event.isComposing&&['INPUT','SELECT'].includes(event.target.tagName)){event.preventDefault();dispatch();}});})();
</script>
</body></html>