Free html code | Decimal to fraction convertor HTML code
Here is a code for Decimal to fraction convertor HTML code
<html lang="en">
<head>
<meta charset="UTF-8"></meta>
<meta content="width=device-width, initial-scale=1.0" name="viewport"></meta>
<title>Decimal to Fraction Converter</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
</style>
</head>
<body>
<h2>Decimal to Fraction Converter</h2>
<label for="decimalInput">Enter Decimal:</label>
<input id="decimalInput" placeholder="Enter decimal" step="any" type="number" />
<button onclick="convertDecimalToFraction()">Convert</button>
<p id="result"></p>
<script>
function convertDecimalToFraction() {
// Get the decimal input value
var decimalInput = document.getElementById("decimalInput").value;
// Convert decimal to fraction using a library or algorithm
var fraction = decimalToFraction(decimalInput);
// Display the result
document.getElementById("result").innerHTML = "Fraction: " + fraction;
}
// Function to convert decimal to fraction
function decimalToFraction(decimal) {
// Use a library or algorithm to convert decimal to fraction
// Here, we are using a simple algorithm for demonstration purposes
var tolerance = 1.0E-9;
var h1 = 1;
var h2 = 0;
var k1 = 0;
var k2 = 1;
var b = decimal;
do {
var a = Math.floor(b);
var aux = h1;
h1 = a * h1 + h2;
h2 = aux;
aux = k1;
k1 = a * k1 + k2;
k2 = aux;
b = 1 / (b - a);
} while (Math.abs(decimal - h1 / k1) > decimal * tolerance);
return h1 + "/" + k1;
}
</script>
</body>
</html>
Comments
Post a Comment