We will learn how to create a function that accepts a number and convert numbers to words in javascript, typescript and jquery
We will also show you the output of convert 1 to one in JavaScript and convert amount in words using JavaScript Indian rupees and to convert number to lakhs javascript
convert number to words javascript and Jquery
The code below converts decimal numbers to words. This code is compatible with both jquery and Javascript.
function convertNumberToWords(num) {
let singleDigit = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
let specialNumbers = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
let tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
let scales = ['', 'thousand', 'million', 'billion', 'trillion'];
num = num.toString();
num = num.replace(/[\, ]/g, '');
if (num !== parseFloat(num)) return 'not a number';
let decimalIndex = num.indexOf('.');
if (decimalIndex === -1) decimalIndex = num.length;
if (decimalIndex > 15) return 'too big';
let digits = num.split('');
let result = '';
let useScales = 0;
for (let i = 0; i < decimalIndex; i++) {
if ((decimalIndex - i) % 3 === 2) {
if (digits[i] === '1') {
result += specialNumbers[Number(digits[i + 1])] + ' ';
i++;
useScales = 1;
} else if (digits[i] !== 0) {
result += tens[digits[i] - 2] + ' ';
useScales = 1;
}
} else if (digits[i] !== 0) {
result += singleDigit[digits[i]] + ' ';
if ((decimalIndex - i) % 3 === 0) result += 'hundred ';
useScales = 1;
}
if ((decimalIndex - i) % 3 === 1) {
if (useScales) result += scales[(decimalIndex - i - 1) / 3] + ' ';
useScales = 0;
}
}
if (decimalIndex !== num.length) {
let endIndex = num.length;
result += 'point ';
for (let i = decimalIndex + 1; i < endIndex; i++) result += singleDigit[digits[i]] + ' ';
}
result = result.replace(/\number+/g, ' ');
return result.trim() + '.';
}
//Call the function
let number = 52642;
console.log(convertNumberToWords(number));
Output
//The function would convert the number 526 to "five hundred twenty six."
>> console.log(convertNumberToWords(526));
< five hundred twenty six.
>> console.log(convertNumberToWords(3.84));
< three point eight four.
//convert 1 to one in JavaScript
>> console.log(convertNumberToWords(1));
< one
//convert numbers to words in lakh
>> console.log(convertNumberToWords(8433484));
< eight million four hundred thirty-three thousand four hundred eighty-four.
//passing the invalid number or text it returns not a number
>> console.log(convertNumberToWords("world"));
< not a number
Convert number to words reactjs
import React, { useState } from 'react';
function ConvertNumberToWords({ num }) {
let singleDigit = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
let specialNumbers = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
let tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
let scales = ['', 'thousand', 'million', 'billion', 'trillion'];
num = num.toString();
num = num.replace(/[\, ]/g, '');
if (num !== parseFloat(num)) return 'not a number';
let decimalIndex = num.indexOf('.');
if (decimalIndex === -1) decimalIndex = num.length;
if (decimalIndex > 15) return 'too big';
let digits = num.split('');
let result = '';
let useScales = 0;
for (let i = 0; i < decimalIndex; i++) {
if ((decimalIndex - i) % 3 === 2) {
if (digits[i] === '1') {
result += specialNumbers[Number(digits[i + 1])] + ' ';
i++;
useScales = 1;
} else if (digits[i] !== 0) {
result += tens[digits[i] - 2] + ' ';
useScales = 1;
}
} else if (digits[i] !== 0) {
result += singleDigit[digits[i]] + ' ';
if ((decimalIndex - i) % 3 === 0) result += 'hundred ';
useScales = 1;
}
if ((decimalIndex - i) % 3 === 1) {
if (useScales) result += scales[(decimalIndex - i - 1) / 3] + ' ';
useScales = 0;
}
}
if (decimalIndex !== num.length) {
let endIndex = num.length;
result += 'point ';
for (let i = decimalIndex + 1; i < endIndex; i++) result += singleDigit[digits[i]] + ' ';
}
result = result.replace(/\number+/g, ' ');
return result.trim() + '.';
}
function App() {
const [number, setNumber] = useState(52642);
return (
<div>
<input type="text" value={number} onChange={e => setNumber(e.target.value)} />
<p>{ConvertNumberToWords({ num: number })}</p>
</div>
);
}
export default App;
Convert numbers to words in TypeScript and Angular
Let's rewrite the code to convert numbers to English words in TypeScript. The following code is compatible with TypeScript and Angular.
function convertNumberToWords(num: number | string): string {
const singleDigit = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const specialNumbers = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
const tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
const scales = ['', 'thousand', 'million', 'billion', 'trillion'];
const numStr = num.toString();
const withoutComma = numStr.replace(/[\, ]/g, '');
if (!Number(withoutComma)) return 'not a number';
const decimalIndex = withoutComma.indexOf('.');
if (decimalIndex === -1) decimalIndex = withoutComma.length;
if (decimalIndex > 15) return 'too big';
const digits = withoutComma.split('');
let result = '';
let useScales = 0;
for (let i = 0; i < decimalIndex; i++) {
if ((decimalIndex - i) % 3 === 2) {
if (digits[i] === '1') {
result += specialNumbers[Number(digits[i + 1])] + ' ';
i++;
useScales = 1;
} else if (digits[i] !== '0') {
result += tens[Number(digits[i]) - 2] + ' ';
useScales = 1;
}
} else if (digits[i] !== '0') {
result += singleDigit[Number(digits[i])] + ' ';
if ((decimalIndex - i) % 3 === 0) result += 'hundred ';
useScales = 1;
}
if ((decimalIndex - i) % 3 === 1) {
if (useScales) result += scales[(decimalIndex - i - 1) / 3] + ' ';
useScales = 0;
}
}
if (decimalIndex !== withoutComma.length) {
const endIndex = withoutComma.length;
result += 'point ';
for (let i = decimalIndex + 1; i < endIndex; i++) result += singleDigit[Number(digits[i])] + ' ';
}
result = result.replace(/\s+/g, ' ');
return result.trim() + '.';
}
Explantionation to above code
A number is passed as a parameter to the function convertNumberToWords
, which transforms the number to words. The words are presented as a string in the English language.
The function first defines arrays of single digits, special numbers (for integers between 10 and 19), tens, and scales (for multiples of thousands such as million, billion, trillion).
The function then transforms the number into a string, removes any commas or spaces, and determines whether the output is still a number by testing it for validity. It returns the string "not a number" if it is not a valid number.
The next step is for the function to determine whether the number has a decimal point and whether it is too lengthy (more than 15 digits).
The function then loops through each digit of the number and adds the relevant English word to a result string. The special number for that two-digit number is added if a digit is 1 and the following digit is between 0 and 9. If the digit is neither 1 nor 0, it adds the equivalent word of tens or single digits. If the number is 0, nothing happens.
Finally, the code inserts any decimal part of the number and returns the result string, removing any extra spaces and adding a period at the end.
Summary
When a number is entered into the convertNumberToWords
function, it is converted to a string, removed of any commas or spaces, checked to see if it is a valid number, and if it is, it is converted to words by breaking it down into individual digits and using arrays of words for single digits, special numbers, tens, and scales to build the result string. The string "not a number" is returned if the input is an invalid number. The function outputs a string with the final word ended by a period.