Open In App

Design a Decimal to Binary Converter in VueJS

The Decimal to Binary Converter is a simple web application built using Vue.js that allows users to convert decimal numbers to binary format. It provides a user-friendly interface for the easy conversion of decimal numbers into a binary representation.

Prerequisites

Approach

CDN link:

https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js

Example: This example shows the implementation of the above-explained approach.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        The Decimal to Binary Converter
    </title>
    <script src=
"https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js">
    </script>
    <link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css">
</head>

<body class="bg-gray-200 py-10">
    <div id="app" class="max-w-md mx-auto 
        bg-white p-8 rounded shadow-md">
        <h1 class="text-2xl font-semibold mb-4">
            Decimal to Binary Converter
        </h1>
        <div class="mb-4">
            <label for="decimalInput" class="block mb-1">
                Enter Decimal Number:
            </label>
            <input type="number" id="decimalInput" 
                v-model.number="decimalNumber"
                class="w-full px-4 py-2 border rounded 
                    focus:outline-none focus:border-blue-500"
                placeholder="Enter decimal number">
        </div>
        <button @click="convertToBinary"
            class="bg-blue-500 text-white px-4 py-2 
                rounded hover:bg-blue-600">
                Convert
            </button>
        <div v-if="binaryNumber" class="mt-4">
            <p>
                <strong>
                    Binary Equivalent:
                </strong> 
                {{ binaryNumber }}
            </p>
        </div>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                decimalNumber: null,
                binaryNumber: null
            },
            methods: {
                convertToBinary() {
                    if (!this.decimalNumber) {
                        alert('Please enter a decimal number');
                        return;
                    }
                    const decimal = parseInt(this.decimalNumber);
                    if (isNaN(decimal)) {
                        alert(`Invalid input. Please enter a
                               valid decimal number`);
                        return;
                    }
                    this.binaryNumber = decimal.toString(2);
                }
            }
        });
    </script>
</body>

</html>

Output:

scc1

Article Tags :