Decimal to Binary Converter Tool Create Using JavaScript With HTML

Hello Friends Today, through this tutorial, I will tell you How to Decimal to Binary Converter Tool Create Using JavaScript With HTML? Here’s a simple example of a Decimal to Binary Converter using HTML and JavaScript. Copy and paste the following code into an HTML file (e.g., `decimal_to_binary_converter.html`) and open it in a web browser:

decimal_to_binary_converter.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Decimal to Binary Converter</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h2 {
color: #333;
}
input[type="number"], input[type="button"] {
padding: 10px;
margin: 5px;
}
#result {
font-weight: bold;
color: #008000;
}
</style>
</head>
<body>

<h2>Decimal to Binary Converter</h2>
<label for="decimalInput">Enter Decimal Number:</label>
<input type="number" id="decimalInput" placeholder="Enter decimal number" />
<input type="button" value="Convert to Binary" onclick="convertToBinary()" />

<h3>Binary Representation:</h3>
<div id="result"></div>

<script>
function convertToBinary() {

// Get decimal input value
var decimalInput = document.getElementById("decimalInput").value;

// Check if the input is a valid decimal number
if (!isValidDecimal(decimalInput)) {
alert("Invalid decimal input. Please enter a valid decimal number.");
return;
}

// Convert decimal to binary
var binaryResult = decimalToBinary(decimalInput);

// Display the result
document.getElementById("result").textContent = + binaryResult;
}

function isValidDecimal(value) {
// Check if the value is a valid decimal number
return /^-?\d+$/.test(value);
}

function decimalToBinary(decimal) {
// Convert decimal to binary
return (decimal >>> 0).toString(2);
}
</script>
</body>
</html>

This Decimal to Binary Converter takes a decimal input number, performs the conversion to binary, and displays the result. It includes basic input validation to ensure that the entered value is a valid decimal number.