react-number-to-words
Version:
Converts number to words.
130 lines (123 loc) • 4.73 kB
JavaScript
import * as React from 'react';
// Constants for number words.
var units = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
// Array of tens as words
var tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
// Array of scales as words
var scales = ['', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion', 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion', 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quatttuor-decillion', 'Quindecillion', 'Sexdecillion', 'Septen-decillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion', 'Centillion'];
// Convert a number to words
var convertToWords = function (number, join) {
if (join === void 0) { join = 'and'; }
var stringNumber = number.toString();
var start;
var end;
var chunks;
var chunksLen;
var chunk;
var ints;
var i;
var word;
var words = [];
// If the number is 0, return 'Zero'
if (parseInt(stringNumber) === 0) {
return 'Zero';
}
// Number upto 99 (0-99)
if (number < 100) {
if (number < 20) {
return units[number];
}
return tens[Math.floor(number / 10)] + (number % 10 ? '-' + units[number % 10] : '');
}
/* Split the number into chunks of 3 digits */
start = stringNumber.length;
chunks = [];
while (start > 0) {
end = start;
chunks.push(stringNumber.slice((start = Math.max(0, start - 3)), end));
}
/* Check if the number is greater than the scales */
chunksLen = chunks.length;
if (chunksLen > scales.length) {
return '';
}
// Convert each chunk to words
words = [];
for (i = 0; i < chunksLen; i++) {
chunk = parseInt(chunks[i]);
if (chunk) {
// Split chunk into array of individual integers
ints = chunks[i].split('').reverse().map(parseFloat);
// If tens integer is 1, i.e. 10, then add 10 to units integer
if (ints[1] === 1) {
ints[0] += 10;
}
// Add scale word if chunk is not zero and array item exists
if ((word = scales[i])) {
words.push(word);
}
// Add unit word if array item exists
if ((word = units[ints[0]])) {
words.push(word);
}
// Add tens word if array item exists
if ((word = tens[ints[1]])) {
words.push(word);
}
// Add 'join' value after the hundreds place
if (ints[0] || ints[1]) {
// Add 'join' value after the hundreds place
if (ints[2] || !i && chunksLen) {
words.push(join);
}
}
// Add hundreds word if array item exists
if ((word = units[ints[2]])) {
words.push(word + ' Hundred');
}
}
}
return words.reverse().join(' ');
};
var toOrdinal = function (number) {
var stringNumber = number.toString();
var lastTwoDigits = stringNumber.slice(-2);
var lastDigit = parseInt(lastTwoDigits.slice(-1));
var lastTwo = parseInt(lastTwoDigits);
var suffixes = ['th', 'st', 'nd', 'rd'];
if (lastTwo >= 11 && lastTwo <= 13) {
return "".concat(number, "th");
}
return "".concat(number).concat(suffixes[lastDigit] || 'th');
};
var NumberInput = function (_a) {
var onChange = _a.onChange;
var _b = React.useState(''), value = _b[0], setValue = _b[1];
var handleChange = function (event) {
var newValue = event.target.value;
if (!isNaN(Number(newValue))) {
setValue(newValue);
onChange(Number(newValue));
}
};
return (React.createElement("div", null,
React.createElement("label", { htmlFor: "number" }, "Enter an number:"),
React.createElement("input", { id: "number", type: "number", value: value, onChange: handleChange, style: {
display: 'block',
width: '100%',
padding: '10px',
fontSize: '16px',
marginTop: '10px',
} })));
};
var NumberDisplay = function (_a) {
var number = _a.number;
return (React.createElement("div", null,
React.createElement("p", null,
"Number entered: ",
number),
React.createElement("p", null,
"Number in words: ",
convertToWords(number))));
};
export { NumberDisplay, NumberInput, convertToWords, toOrdinal };