{"id":1717,"date":"2024-12-26T15:49:07","date_gmt":"2024-12-26T15:49:07","guid":{"rendered":"https:\/\/donhit.com\/en\/?p=1717"},"modified":"2025-02-07T08:18:13","modified_gmt":"2025-02-07T08:18:13","slug":"fraction","status":"publish","type":"post","link":"https:\/\/donhit.com\/en\/calculator\/fraction\/","title":{"rendered":"Fraction Calculator"},"content":{"rendered":"<p><center><div class=\"calculator\">\r\n        <h2>Fraction Calculator Tool<\/h2>\r\n        <div class=\"input-group\">\r\n            <div class=\"fraction-input\">\r\n                <input type=\"number\" id=\"num1\" placeholder=\"0\" aria-label=\"First numerator\">\r\n                <div class=\"fraction-line\"><\/div>\r\n                <input type=\"number\" id=\"den1\" placeholder=\"1\" aria-label=\"First denominator\">\r\n            <\/div>\r\n            <select id=\"operator\" aria-label=\"Operation\">\r\n                <option value=\"+\">+<\/option>\r\n                <option value=\"-\">\u2212<\/option>\r\n                <option value=\"*\">\u00d7<\/option>\r\n                <option value=\"\/\">\u00f7<\/option>\r\n            <\/select>\r\n            <div class=\"fraction-input\">\r\n                <input type=\"number\" id=\"num2\" placeholder=\"0\" aria-label=\"Second numerator\">\r\n                <div class=\"fraction-line\"><\/div>\r\n                <input type=\"number\" id=\"den2\" placeholder=\"1\" aria-label=\"Second denominator\">\r\n            <\/div>\r\n        <\/div>\r\n        <button onclick=\"calculate()\">Calculate<\/button>\r\n        <div class=\"result\">\r\n            <h2>Result<\/h2>\r\n            <div class=\"fraction-result\" id=\"result\">0\/1<\/div>\r\n        <\/div>\r\n        <div id=\"error\" class=\"error\"><\/div>\r\n    <\/div>\r\n\r\n    <script>\r\n        function gcd(a, b) {\r\n            a = Math.abs(a);\r\n            b = Math.abs(b);\r\n            while (b) {\r\n                let t = b;\r\n                b = a % b;\r\n                a = t;\r\n            }\r\n            return a;\r\n        }\r\n\r\n        function reduceFraction(numerator, denominator) {\r\n            const divisor = gcd(numerator, denominator);\r\n            return [numerator \/ divisor, denominator \/ divisor];\r\n        }\r\n\r\n        function calculate() {\r\n            const num1 = parseInt(document.getElementById('num1').value) || 0;\r\n            const den1 = parseInt(document.getElementById('den1').value) || 1;\r\n            const num2 = parseInt(document.getElementById('num2').value) || 0;\r\n            const den2 = parseInt(document.getElementById('den2').value) || 1;\r\n            const operator = document.getElementById('operator').value;\r\n            const errorElement = document.getElementById('error');\r\n            const resultElement = document.getElementById('result');\r\n\r\n            \/\/ Reset error state\r\n            errorElement.classList.remove('visible');\r\n\r\n            \/\/ Validate denominators\r\n            if (den1 === 0 || den2 === 0) {\r\n                errorElement.textContent = \"Denominator cannot be zero\";\r\n                errorElement.classList.add('visible');\r\n                resultElement.textContent = \"Undefined\";\r\n                return;\r\n            }\r\n\r\n            let resultNum, resultDen;\r\n\r\n            try {\r\n                switch (operator) {\r\n                    case '+':\r\n                        resultNum = num1 * den2 + num2 * den1;\r\n                        resultDen = den1 * den2;\r\n                        break;\r\n                    case '-':\r\n                        resultNum = num1 * den2 - num2 * den1;\r\n                        resultDen = den1 * den2;\r\n                        break;\r\n                    case '*':\r\n                        resultNum = num1 * num2;\r\n                        resultDen = den1 * den2;\r\n                        break;\r\n                    case '\/':\r\n                        if (num2 === 0) {\r\n                            throw new Error(\"Cannot divide by zero\");\r\n                        }\r\n                        resultNum = num1 * den2;\r\n                        resultDen = den1 * num2;\r\n                        break;\r\n                }\r\n\r\n                \/\/ Handle negative denominators\r\n                if (resultDen < 0) {\r\n                    resultNum = -resultNum;\r\n                    resultDen = -resultDen;\r\n                }\r\n\r\n                \/\/ Reduce the fraction\r\n                [resultNum, resultDen] = reduceFraction(resultNum, resultDen);\r\n\r\n                \/\/ Format the result\r\n                resultElement.textContent = `${resultNum}\/${resultDen}`;\r\n\r\n            } catch (error) {\r\n                errorElement.textContent = error.message;\r\n                errorElement.classList.add('visible');\r\n                resultElement.textContent = \"Undefined\";\r\n            }\r\n        }\r\n\r\n        \/\/ Add keyboard support\r\n        document.addEventListener('keypress', function(e) {\r\n            if (e.key === 'Enter') {\r\n                calculate();\r\n            }\r\n        });\r\n\r\n        \/\/ Prevent negative values in denominators\r\n        document.getElementById('den1').addEventListener('input', function(e) {\r\n            if (e.target.value < 0) e.target.value = Math.abs(e.target.value);\r\n        });\r\n\r\n        document.getElementById('den2').addEventListener('input', function(e) {\r\n            if (e.target.value < 0) e.target.value = Math.abs(e.target.value);\r\n        });\r\n    <\/script><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[],"class_list":["post-1717","post","type-post","status-publish","format-standard","hentry","category-calculator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Fraction Calculator - DonHit<\/title>\n<meta name=\"description\" content=\"Fraction calculators are essential for simplifying fractions, converting between mixed and improper fractions, and finding equivalent fractions\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/donhit.com\/en\/calculator\/fraction\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fraction Calculator - DonHit\" \/>\n<meta property=\"og:description\" content=\"Fraction calculators are essential for simplifying fractions, converting between mixed and improper fractions, and finding equivalent fractions\" \/>\n<meta property=\"og:url\" content=\"https:\/\/donhit.com\/en\/calculator\/fraction\/\" \/>\n<meta property=\"og:site_name\" content=\"DonHit - World of Tools\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-26T15:49:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-07T08:18:13+00:00\" \/>\n<meta name=\"author\" content=\"DonHit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DonHit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fraction Calculator - DonHit","description":"Fraction calculators are essential for simplifying fractions, converting between mixed and improper fractions, and finding equivalent fractions","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/donhit.com\/en\/calculator\/fraction\/","og_locale":"en_US","og_type":"article","og_title":"Fraction Calculator - DonHit","og_description":"Fraction calculators are essential for simplifying fractions, converting between mixed and improper fractions, and finding equivalent fractions","og_url":"https:\/\/donhit.com\/en\/calculator\/fraction\/","og_site_name":"DonHit - World of Tools","article_published_time":"2024-12-26T15:49:07+00:00","article_modified_time":"2025-02-07T08:18:13+00:00","author":"DonHit","twitter_card":"summary_large_image","twitter_misc":{"Written by":"DonHit","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/donhit.com\/en\/calculator\/fraction\/#article","isPartOf":{"@id":"https:\/\/donhit.com\/en\/calculator\/fraction\/"},"author":{"name":"DonHit","@id":"https:\/\/donhit.com\/en\/#\/schema\/person\/0c6ff7dcd8ba4810c56a532f09c33148"},"headline":"Fraction Calculator","datePublished":"2024-12-26T15:49:07+00:00","dateModified":"2025-02-07T08:18:13+00:00","mainEntityOfPage":{"@id":"https:\/\/donhit.com\/en\/calculator\/fraction\/"},"wordCount":8,"commentCount":0,"publisher":{"@id":"https:\/\/donhit.com\/en\/#\/schema\/person\/0c6ff7dcd8ba4810c56a532f09c33148"},"articleSection":["Calculator"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/donhit.com\/en\/calculator\/fraction\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/donhit.com\/en\/calculator\/fraction\/","url":"https:\/\/donhit.com\/en\/calculator\/fraction\/","name":"Fraction Calculator - DonHit","isPartOf":{"@id":"https:\/\/donhit.com\/en\/#website"},"datePublished":"2024-12-26T15:49:07+00:00","dateModified":"2025-02-07T08:18:13+00:00","description":"Fraction calculators are essential for simplifying fractions, converting between mixed and improper fractions, and finding equivalent fractions","breadcrumb":{"@id":"https:\/\/donhit.com\/en\/calculator\/fraction\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/donhit.com\/en\/calculator\/fraction\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/donhit.com\/en\/calculator\/fraction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Trang ch\u1ee7","item":"https:\/\/donhit.com\/en\/"},{"@type":"ListItem","position":2,"name":"Calculator","item":"https:\/\/donhit.com\/en\/category\/calculator\/"},{"@type":"ListItem","position":3,"name":"Fraction Calculator"}]},{"@type":"WebSite","@id":"https:\/\/donhit.com\/en\/#website","url":"https:\/\/donhit.com\/en\/","name":"DonHit - World of tools","description":"","publisher":{"@id":"https:\/\/donhit.com\/en\/#\/schema\/person\/0c6ff7dcd8ba4810c56a532f09c33148"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/donhit.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/donhit.com\/en\/#\/schema\/person\/0c6ff7dcd8ba4810c56a532f09c33148","name":"DonHit","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/donhit.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/donhit.com\/en\/wp-content\/uploads\/2024\/11\/logo-donhit.webp","contentUrl":"https:\/\/donhit.com\/en\/wp-content\/uploads\/2024\/11\/logo-donhit.webp","width":400,"height":267,"caption":"DonHit"},"logo":{"@id":"https:\/\/donhit.com\/en\/#\/schema\/person\/image\/"},"description":"DonHit is a website designed to provide useful tools for everyone. Its primary goal is to support and empower the community. All the tools available on the site are completely free to use.","sameAs":["https:\/\/donhit.com\/en"],"url":"https:\/\/donhit.com\/en\/author\/admin_don\/"}]}},"_links":{"self":[{"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/posts\/1717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/comments?post=1717"}],"version-history":[{"count":3,"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/posts\/1717\/revisions"}],"predecessor-version":[{"id":2074,"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/posts\/1717\/revisions\/2074"}],"wp:attachment":[{"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/media?parent=1717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/categories?post=1717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/donhit.com\/en\/wp-json\/wp\/v2\/tags?post=1717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}