Fraction Calculator with Steps body { font-family: Arial, sans-serif; margin: 0; background-color: #f0f0f0; } #headerout { background-color: #007bff; color: white; } #header { max-width: 1000px; margin: 0 auto; padding: 10px; display: flex; justify-content: space-between; align-items: center; } #logo a { color: white; text-decoration: none; font-size: 22px; font-weight: bold; } #contentout { background-color: white; } #content { max-width: 1000px; margin: 0 auto; padding: 20px; } h1, h2 { color: #333; } .panel { border: 1px solid #ccc; padding: 15px; border-radius: 5px; background-color: #f9f9f9; margin-bottom: 20px; } .innormal { width: 80px; text-align: center; font-size: 16px; padding: 5px; } .inlonger { width: 120px; text-align: center; font-size: 16px; padding: 5px; } .inlongest { width: 200px; text-align: center; font-size: 16px; padding: 5px; } .verybigtext { font-size: 24px; font-weight: bold; padding: 0 10px; } input[type="text"] { border: 1px solid #ccc; border-radius: 3px; } input[type="submit"], input[type="button"] { background-color: #007bff; color: white; border: none; padding: 10px 15px; font-size: 16px; cursor: pointer; border-radius: 3px; margin: 5px; } input[type="button"] { background-color: #6c757d; } input[type="submit"]:hover { background-color: #0056b3; } input[type="button"]:hover { background-color: #5a6268; } .frac { display: inline-block; text-align: center; vertical-align: middle; font-size: 18px; margin: 0 4px; } .fracden { border-top: 1px solid black; display: block; padding-top: 2px; } .fracnum { padding-bottom: 2px; display: block; } .steps-container { border: 1px solid #ddd; margin-top: 20px; padding: 15px; background-color: #fdfdfd; border-radius: 5px; display: none; /* Hidden by default */ } .steps-container h3 { margin-top: 0; color: #0056b3; border-bottom: 1px solid #eee; padding-bottom: 5px; } .steps-container p { margin: 10px 0; line-height: 1.6; }

Fraction Calculator

Below are multiple fraction calculators capable of addition, subtraction, multiplication, division, simplification, and conversion between fractions and decimals. Fields above the solid black line represent the numerator, while fields below represent the denominator.

+ - × ÷ = ?
?

Mixed Numbers Calculator

+ - × ÷ = ?

Simplify Fractions Calculator

= ?

Decimal to Fraction Calculator

= ?
?

Fraction to Decimal Calculator

= ?
// --- UTILITY FUNCTIONS --- function gcd(a, b) { return b === 0 ? a : gcd(b, a % b); } function lcm(a, b) { return Math.abs(a * b) / gcd(a, b) || 0; } function generateFractionHtml(num, den) { return `${num}${den}`; } function parseMixedNumber(str) { str = String(str).trim(); const isNegative = str.startsWith('-'); if (isNegative) str = str.substring(1); let whole = 0, num = 0, den = 1; const parts = str.split(/[ /]/).filter(p => p.trim() !== ""); if (parts.length === 1) { // "5" or "3/4" if (str.includes('/')) { num = parseInt(parts[0], 10); den = parseInt(parts[1], 10); } else { whole = parseInt(parts[0], 10); } } else if (parts.length === 2) { // "1 2" (assuming 1 2/?) is not valid, so must be "3/4" with space) or "1 1/2" if (str.includes('/')) { // like "1 1/2" whole = parseInt(parts[0], 10); const fracParts = parts[1].split('/'); num = parseInt(fracParts[0], 10); den = parseInt(fracParts[1], 10); } else if (str.includes(' ')) { // "1 1/2" case whole = parseInt(parts[0], 10); num = parseInt(parts[1].split('/')[0], 10); den = parseInt(parts[1].split('/')[1], 10); } else { // "3/4" num = parseInt(parts[0], 10); den = parseInt(parts[1], 10); } } if (parts.length === 3) { // "1 2 / 3" whole = parseInt(parts[0], 10); num = parseInt(parts[1], 10); den = parseInt(parts[2], 10); } let totalNum = (whole * den) + num; if (isNegative) totalNum = -totalNum; if (isNaN(totalNum) || isNaN(den) || den === 0) return { num: NaN, den: NaN }; return { num: totalNum, den }; } function toMixedNumberString(num, den) { if (den === 0) return "Undefined"; if (num === 0) return "0"; const common = gcd(Math.abs(num), Math.abs(den)); num /= common; den /= common; if (den < 0) { num = -num; den = -den; } const isNegative = num 0) result += whole; if (remainder > 0) { if (whole > 0) result += " "; result += `${remainder}/${den}`; } if (result === "" || result === "-") return "0"; return result; } function clearForm(form, resultIds, stepContainerId) { form.reset(); resultIds.forEach(id => { const el = document.getElementById(id); if(el) { if (el.tagName === 'TD' || el.tagName === 'SPAN') { el.innerHTML = id.includes('_b') ? '?' : (id.includes('=') ? '= ?' : '?'); } else { el.innerHTML = '?'; } } }); if (stepContainerId) { const stepEl = document.getElementById(stepContainerId); stepEl.innerHTML = ''; stepEl.style.display = 'none'; } } // --- CALCULATION LOGIC --- // Main fraction calculator document.getElementById('calform').addEventListener('submit', function(e) { e.preventDefault(); const t1 = parseInt(this.t1.value), b1 = parseInt(this.b1.value); const t2 = parseInt(this.t2.value), b2 = parseInt(this.b2.value); const op = this.op.value; if ([t1, b1, t2, b2].some(isNaN) || b1 === 0 || b2 === 0) return alert("Invalid input."); let res_t, res_b; let stepsHtml = '

Calculation Steps:

'; const opChar = op; const f1Html = generateFractionHtml(t1, b1); const f2Html = generateFractionHtml(t2, b2); stepsHtml += `

Problem: ${f1Html} ${opChar} ${f2Html}

`; if (op === '+' || op === '-') { const common_den = lcm(b1, b2); const new_t1 = t1 * (common_den / b1); const new_t2 = t2 * (common_den / b2); res_t = (op === '+') ? new_t1 + new_t2 : new_t1 - new_t2; res_b = common_den; stepsHtml += `

1. Find the least common denominator (LCD) of ${b1} and ${b2}: LCD = ${common_den}.

`; stepsHtml += `

2. Rewrite fractions with the LCD:

`; stepsHtml += `

${f1Html} = ${generateFractionHtml(new_t1, common_den)}

`; stepsHtml += `

${f2Html} = ${generateFractionHtml(new_t2, common_den)}

`; stepsHtml += `

3. Perform the operation: ${generateFractionHtml(new_t1, common_den)} ${opChar} ${generateFractionHtml(new_t2, common_den)} = ${generateFractionHtml(res_t, res_b)}

`; } else if (op === '*') { res_t = t1 * t2; res_b = b1 * b2; stepsHtml += `

1. Multiply the numerators: ${t1} × ${t2} = ${res_t}

`; stepsHtml += `

2. Multiply the denominators: ${b1} × ${b2} = ${res_b}

`; stepsHtml += `

3. Resulting fraction: ${generateFractionHtml(res_t, res_b)}

`; } else if (op === '/') { res_t = t1 * b2; res_b = b1 * t2; stepsHtml += `

1. "Keep, Change, Flip": Keep the first fraction, change division to multiplication, and flip the second fraction.

`; stepsHtml += `

${f1Html} × ${generateFractionHtml(b2, t2)}

`; stepsHtml += `

2. Multiply: ${generateFractionHtml(res_t, res_b)}

`; } const common = gcd(Math.abs(res_t), Math.abs(res_b)); if (common > 1) { stepsHtml += `

4. Simplify the fraction: The GCD of ${Math.abs(res_t)} and ${Math.abs(res_b)} is ${common}. Divide both by ${common}.

`; stepsHtml += `

${generateFractionHtml(res_t, res_b)} = ${generateFractionHtml(res_t / common, res_b / common)}

`; } const final_t = res_t / common; const final_b = res_b / common; document.getElementById('res_t1').innerText = final_b < 0 ? -final_t : final_t; document.getElementById('res_b1').innerText = final_b < 0 ? -final_b : final_b; document.getElementById('steps-calform').innerHTML = stepsHtml; document.getElementById('steps-calform').style.display = 'block'; }); // Mixed numbers calculator document.getElementById('calform5').addEventListener('submit', function(e) { e.preventDefault(); const f1_str = this.c5num1.value; const f2_str = this.c5num2.value; const f1 = parseMixedNumber(f1_str); const f2 = parseMixedNumber(f2_str); if (isNaN(f1.num) || isNaN(f2.num)) return alert("Invalid mixed number format."); let stepsHtml = '

Calculation Steps:

'; stepsHtml += `

1. Convert mixed numbers to improper fractions:

`; stepsHtml += `

'${f1_str}' becomes ${generateFractionHtml(f1.num, f1.den)}

`; stepsHtml += `

'${f2_str}' becomes ${generateFractionHtml(f2.num, f2.den)}

`; // Now reuse the logic from the main calculator const t1 = f1.num, b1 = f1.den, t2 = f2.num, b2 = f2.den; const op = this.c5op.value; let res_t, res_b; stepsHtml += `

2. Perform the operation on improper fractions: ${generateFractionHtml(t1, b1)} ${op} ${generateFractionHtml(t2, b2)}

`; // (Calculation steps for improper fractions would go here, similar to above) switch (op) { case '+': res_t = t1 * b2 + t2 * b1; res_b = b1 * b2; break; case '-': res_t = t1 * b2 - t2 * b1; res_b = b1 * b2; break; case '*': res_t = t1 * t2; res_b = b1 * b2; break; case '/': res_t = t1 * b2; res_b = b1 * t2; break; } stepsHtml += `

3. Resulting fraction: ${generateFractionHtml(res_t, res_b)}

`; const finalResultStr = toMixedNumberString(res_t, res_b); stepsHtml += `

4. Simplify and convert back to a mixed number: ${finalResultStr}

`; document.getElementById('res_mixed').innerText = `= ${finalResultStr}`; document.getElementById('steps-calform5').innerHTML = stepsHtml; document.getElementById('steps-calform5').style.display = 'block'; }); // Simplify fractions calculator document.getElementById('calform4').addEventListener('submit', function(e) { e.preventDefault(); const num = parseInt(this.c4t1.value); const den = parseInt(this.c4b1.value); if (isNaN(num) || isNaN(den) || den === 0) return alert("Invalid input."); let stepsHtml = `

Calculation Steps:

`; stepsHtml += `

1. The fraction is: ${generateFractionHtml(num, den)}

`; const common = gcd(Math.abs(num), Math.abs(den)); stepsHtml += `

2. Find the Greatest Common Divisor (GCD) of ${Math.abs(num)} and ${Math.abs(den)}. The GCD is ${common}.

`; const finalNum = num / common; const finalDen = den / common; stepsHtml += `

3. Divide both numerator and denominator by the GCD:

`; stepsHtml += `

${generateFractionHtml(num, den)} ÷ ${generateFractionHtml(common, common)} = ${generateFractionHtml(finalNum, finalDen)}

`; const finalResultStr = toMixedNumberString(finalNum, finalDen); if (Math.abs(finalNum) > finalDen) { stepsHtml += `

4. Convert to a mixed number: ${finalResultStr}

`; } document.getElementById('res_simplify').innerText = `= ${finalResultStr}`; document.getElementById('steps-calform4').innerHTML = stepsHtml; document.getElementById('steps-calform4').style.display = 'block'; }); // Decimal to fraction calculator document.getElementById('calform2').addEventListener('submit', function(e) { e.preventDefault(); const decimalStr = this.c2d1.value; const decimal = parseFloat(decimalStr); if (isNaN(decimal)) return alert("Invalid decimal."); let stepsHtml = `

Calculation Steps:

`; const len = decimalStr.includes('.') ? decimalStr.split('.')[1].length : 0; const den = Math.pow(10, len); const num = Math.round(decimal * den); // Use round to handle floating point issues stepsHtml += `

1. Write the decimal as a fraction: ${generateFractionHtml(decimalStr, 1)}

`; stepsHtml += `

2. Multiply numerator and denominator by 10${len} (${den}) to remove the decimal:

`; stepsHtml += `

${generateFractionHtml(decimalStr + ' × ' + den, '1 × ' + den)} = ${generateFractionHtml(num, den)}

`; const common = gcd(Math.abs(num), den); stepsHtml += `

3. Simplify by dividing by the GCD (${common}): ${generateFractionHtml(num / common, den / common)}

`; document.getElementById('res_t2').innerText = num / common; document.getElementById('res_b2').innerText = den / common; document.getElementById('steps-calform2').innerHTML = stepsHtml; document.getElementById('steps-calform2').style.display = 'block'; }); // Fraction to decimal calculator document.getElementById('calform3').addEventListener('submit', function(e) { e.preventDefault(); const num = parseInt(this.c3t1.value); const den = parseInt(this.c3b1.value); if (isNaN(num) || isNaN(den) || den === 0) return alert("Invalid input."); let stepsHtml = `

Calculation Steps:

`; stepsHtml += `

1. To convert a fraction to a decimal, divide the numerator by the denominator.

`; stepsHtml += `

${num} ÷ ${den} = ${num / den}

`; document.getElementById('res_decimal').innerText = `= ${num / den}`; document.getElementById('steps-calform3').innerHTML = stepsHtml; document.getElementById('steps-calform3').style.display = 'block'; });