Number conversion to words in javaScript

Number conversion to words is one of the most essential things on the internet. Using javaScript you can easily convert a number into the corresponding words.
In this post, we will learn how to convert a number into a word using javascript.

What is number conversion to words?

Number conversion to words is the representation of digits in text format.
Example:
Number Format: I have 100$.
Word Format: I have one hundred dollars.

Number conversion to word in website

On the internet, almost every website need to convert number into a word. For example on youtube we see they show the number of views in word format like 1K for 1000 1M for 10000.
So if you want this kind of number conversion feature on your website you can use JavaScript. Using javaScript you can easily create number conversion to word.

JavaScript Number Conversion to word

To convert a number into a word javascript is the best technology. There are several ways in javascript to convert a number into a word.
But in this post, we will learn two ways to convert numbers into words.

  1. Number conversion to words using raw javascript.
  2. Number conversion to words using jquery library.

Number conversion to words using raw javascript

Writing a raw script for number conversion is not so easy. Because there are so many combinations. The main problem is the explanation of this kine problem is so hard. The main idea is you need to calculate from right to left Like single, Decade, hundred, Thousand, etc.

const arr = x => Array.from(x);
const num = x => Number(x) || 0;
const str = x => String(x);
const isEmpty = xs => xs.length === 0;
const take = n => xs => xs.slice(0,n);
const drop = n => xs => xs.slice(n);
const reverse = xs => xs.slice(0).reverse();
const comp = f => g => x => f (g (x));
const not = x => !x;
const chunk = n => xs => isEmpty(xs) ? [] : [take(n)(xs), ...chunk (n) (drop (n) (xs))];

// numToWords :: (Number a, String a) => a -> String
let numToWords = n => {
  let a = [
    '', 'one', 'two', 'three', 'four',
    'five', 'six', 'seven', 'eight', 'nine',
    'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
    'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
  ];
  
  let b = [
    '', '', 'twenty', 'thirty', 'forty',
    'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
  ];
  
  let g = [
    '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion',
    'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion'
  ];
  
  // this part is really nasty still
  // it might edit this again later to show how Monoids could fix this up
  let makeGroup = ([ones,tens,huns]) => {
    return [
      num(huns) === 0 ? '' : a[huns] + ' hundred ',
      num(ones) === 0 ? b[tens] : b[tens] && b[tens] + '-' || '',
      a[tens+ones] || a[ones]
    ].join('');
  };

  let thousand = (group,i) => group === '' ? group : `${group} ${g[i]}`;
  if (typeof n === 'number')
    return numToWords(String(n));
  else if (n === '0')
    return 'zero';
  else
    return comp (chunk(3)) (reverse) (arr(n))
      .map(makeGroup)
      .map(thousand)
      .filter(comp(not)(isEmpty))
      .reverse()
      .join(' ');
};

console.log(numToWords(11009));
//=> eleven thousand nine

console.log(numToWords(10000001));
//=> ten million one 

console.log(numToWords(987));
//=> nine hundred eighty-seven

console.log(numToWords(1015));
//=> one thousand fifteen

console.log(numToWords(55111222333));
//=> fifty-five billion one hundred eleven million two hundred 
//   twenty-two thousand three hundred thirty-three

console.log(numToWords("999999999999999999999991"));
//=> nine hundred ninety-nine sextillion nine hundred ninety-nine
//   quintillion nine hundred ninety-nine quadrillion nine hundred
//   ninety-nine trillion nine hundred ninety-nine billion nine
//   hundred ninety-nine million nine hundred ninety-nine thousand
//   nine hundred ninety-one

console.log(numToWords(6000753512));
//=> six billion seven hundred fifty-three thousand five hundred
//   twelve
This code collect from stackoverflow

Number conversion to word using jquery library

Using the jquery library you can easily convert a number into a word.

Steps of using jquery library for number conversion to word

  1. Download the Jquery or use jquery CDN
  2. Download the jquery number conversion to the word library
  3. Linkup the library script after the jquery
  4. Take an area(div) and give an id of the area
  5. Initialize the area as numberToWord Converter area
  6. Inside the area(div)
    1. takes an input filed for number input with id num & focus this using jquery.
    2. Take a button with id trans
    3. Finally, take an empty div for showing the words
<!-- this is the main area -->
<div id="demo" class="container">
    <h1>jQuery num2words Converter</h1>
    <h3> Enter amount: </h3>
    <!-- for numer input -->
    <input id="num" type="text" class="form-control" placeholder="$"><br>
    <br>
    <!-- calculation will start after clicking on this button -->
    <input id="trans" type="button" value="Convert to words" class="btn btn-danger"><br>
    <br>
    <div class="well">
        <!-- ans will show in this div -->
    </div>
</div>
<!-- end area -->

<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript" language="javascript" src="jquery.num2words.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#num').focus();
        $('#demo').num2words();
    });
</script>    
Still you face problems, feel free to contact with me, I will try my best to help you.