Decimal to Text Converter
Decimal to Text Converter
Convert
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #84fab0, #8fd3f4);
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: white;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
padding: 20px;
text-align: center;
max-width: 400px;
width: 100%;
}
h1 {
margin-bottom: 20px;
color: #4a4a4a;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 2px solid #84fab0;
border-radius: 5px;
font-size: 16px;
}
button {
padding: 10px 15px;
border: none;
border-radius: 5px;
background-color: #8fd3f4;
color: white;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #84fab0;
}
h2 {
margin-top: 20px;
color: #4a4a4a;
}
document.getElementById('convertButton').addEventListener('click', function() {
const decimalInput = document.getElementById('decimalInput').value;
const resultElement = document.getElementById('result');
// Check if input is a valid number
if (isNaN(decimalInput) || decimalInput.trim() === '') {
resultElement.innerText = 'Please enter a valid decimal number.';
return;
}
const decimalNumber = parseFloat(decimalInput);
resultElement.innerText = convertDecimalToText(decimalNumber);
});
function convertDecimalToText(decimal) {
if (decimal < 0) return 'Negative numbers are not supported.';
const ones = [
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
'seventeen', 'eighteen', 'nineteen'
];
const tens = [
'', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
];
if (decimal < 20) return ones[Math.floor(decimal)];
if (decimal < 100) {
const tenPart = Math.floor(decimal / 10);
const onePart = decimal % 10;
return tens[tenPart] + (onePart ? '-' + ones[onePart] : '');
}
if (decimal < 1000) {
const hundredPart = Math.floor(decimal / 100);
const rest = decimal % 100;
return ones[hundredPart] + ' hundred' + (rest ? ' and ' + convertDecimalToText(rest) : '');
}
return 'Numbers larger than 999 are not supported.';
}
No comments:
Post a Comment