Trying to convert numbers to words. It converts non-decimal numbers, but when it comes to decimals it shows undefined
for decimal parts like for number e.g. 118.65. It shows undefined rupees only instead of one hundred eighteen rupees and sixty five paisa only.
- Input given: 118.65
- Actual output: one hundred unedined rupees only
- Expected output: one hundred eighteen Rupees and sixty five paisa only
function App() {
return <Textword grandtotalAmount={118.65} />;
}
function Textword({ grandtotalAmount }) {
const convertNumberToWords = (number) => {
const units = [
"",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
];
const tens = [
"",
"Ten",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety",
];
const teens = [
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
];
const crore = "Crore";
const lakh = "Lakh";
const arab = "Arab";
const currency = "Rupees";
const paisa = "Paisa";
if (number === 0) {
return "Zero " + currency;
}
let words = "";
if (number >= 1000000000) {
words +=
convertNumberToWords(Math.floor(number / 1000000000)) +
" " +
arab +
" ";
number %= 1000000000;
}
if (number >= 10000000) {
words +=
convertNumberToWords(Math.floor(number / 10000000)) + " " + crore + " ";
number %= 10000000;
}
if (number >= 100000) {
words +=
convertNumberToWords(Math.floor(number / 100000)) + " " + lakh + " ";
number %= 100000;
}
if (number >= 1000) {
words += convertNumberToWords(Math.floor(number / 1000)) + " Thousand ";
number %= 1000;
}
if (number >= 100) {
words += convertNumberToWords(Math.floor(number / 100)) + " Hundred ";
number %= 100;
}
if (number >= 20) {
words += tens[Math.floor(number / 10)] + " ";
number %= 10;
} else if (number >= 10) {
console.log(number);
words += teens[number - 10] + " ";
number = 0;
console.log(words);
}
if (number > 0) {
words += units[number] + " ";
}
const decimalPart = Math.round((number % 1) * 100);
console.log(decimalPart);
if (decimalPart > 0) {
words += "and" + decimalPart + " " + paisa + "only ";
}
console.log(words);
return words.trim() + " ";
};
let grandTotal = grandtotalAmount; // Example value
let amountWord = convertNumberToWords(grandTotal);
console.log(amountWord);
return <React.Fragment>{amountWord}</React.Fragment>;
}
ReactDOM
.createRoot(document.getElementById("root"))
.render(<App />);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js"></script>
2
Answers
The following modifications should take place:
words
field should be an arrayconvertNumberToWords
outside of the componentconvertNumberToWords
will need an inner recursive function that considers its own wordsconvert
to handle the decimal portionHere is a working example:
I think you can use
number-to-words
module.pleaze check this url of number to words module.
This is an example: