0

I am having issues with spaces. Also, dashes are needed in between numbers such as fifty-four; however, I am getting dashes behind numbers like fifty- thousand.

Also, I require no blank spaces after the output, but I keep having issues outputting with spaces.

Any ideas?

PS: I have studied Convert digits into words with JavaScript - I would like my version to work.

function number2words(n) {
  a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
  b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
  c = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];


  if ((n = n.toString()).length > 9) return false;
  num = ('000000000' + n).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
  if (!num)
    return;

  var str = '';
  dash = '-';

  if (c[num[5][1]] == '')
    dash = '';

  if (n <= 19 && n != 0)
    return c[n];
  else if (n == 0)
    return 'zero';
  else if (n == 20 || n == 30 || n == 40 || n == 50 || n == 60 || n == 70 || n == 80 || n == 90)
    return b[n[0]];
  else
    str += (num[1] != 0) ? (a[Number(num[1])] || b[num[1][0]] + '-' + a[num[1][1]]) + 'million ' : '';
  str += (num[2] != 0) ? (a[Number(num[2])] || b[num[2][0]] + '-' + a[num[2][1]]) + 'hundred ' : '';
  str += (num[3] != 0) ? (a[Number(num[3])] || b[num[3][0]] + '-' + a[num[3][1]]) + 'thousand ' : '';
  str += (num[4] != 0) ? (a[Number(num[4])] || b[num[4][0]] + '-' + a[num[4][1]]) + 'hundred' : '';
  str += (num[5] != 0) ? ((str != '') ? ' ' : '') + (a[Number(num[5])] || b[num[5][0]] + dash + a[num[5][1]]) : '';
  console.log(str[str.length - 1], str.length);

  if (str[str.length - 1] == " ") {
    var sl = str.slice(0, str.length - 1);
    return sl;
  }
  return str;
}
console.log(number2words(15007));
console.log(number2words(464097));

5
  • stackoverflow.com/questions/5529934/javascript-numbers-to-words Commented Oct 6, 2016 at 15:03
  • So two people found two duplicates in less than one minute. Commented Oct 6, 2016 at 15:04
  • My problem isn't the duplicate. My question is based on the original answer however with edits I need help figuring out how to address small bugs as explained. Commented Oct 6, 2016 at 23:24
  • Please see my updates to your question - it is now a minimal reproducible example. It shows one issue. Can you add more that shows leading and trailing spaces? Commented Oct 7, 2016 at 5:31
  • I have added edits with the numbers to run showing the issue. Commented Oct 7, 2016 at 12:14

2 Answers 2

0

Can you please try bellow.

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

function inWords (num) {
    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return; var str = '';
    str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + '-' + a[n[1][1]]) + 'million ' : '';
    str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + '-' + a[n[2][1]]) + 'hundred ' : '';
    str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + '-' + a[n[3][1]]) + 'thousand ' : '';
    str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + '-' + a[n[4][1]]) + 'hundred ' : '';
    str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + '-' + a[n[5][1]]) + 'only ' : '';
    return str;
}

console.log(inWords(999999999));
console.log(inWords(1095));

Copy Form Convert digits into words with JavaScript Done some changes for your output.

Sign up to request clarification or add additional context in comments.

1 Comment

I can see ninety-nine million ninety-nine hundred ninety-nine thousand nine hundred and ninety-nine only in console.
0

Solved

function number2words(n) {
  a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
  b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
  c = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];


  if ((n = n.toString()).length > 9) return false;
  num = ('000000000' + n).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
  if (!num)
    return;

  var str = '';
  dash = '-';

  if (c[num[5][1]] == '')
    dash = '';

  if (n <= 19 && n != 0)
    return c[n];
  else if (n == 0)
    return 'zero';
  else if (n == 20 || n == 30 || n == 40 || n == 50 || n == 60 || n == 70 || n == 80 || n == 90)
    return b[n[0]];
  else
    str += (num[1] != 0) ? (a[Number(num[1])]  || b[num[1][0]] + '-' + a[num[1][1]]) + 'million ' : '';
    str += (num[2] != 0) ? (a[Number(num[2])]  || b[num[2][0]] + '-' + a[num[2][1]]) + 'hundred ' : '';
    str += (num[3] != 0)  ? (a[Number(num[3])]  || b[num[3][0]] + '-' + a[num[3][1]]) + 'thousand ' : '';
    str += (num[4] != 0) ? (a[Number(num[4])]  || b[num[4][0]] + '-' + a[num[4][1]]) + 'hundred' : '';
    str += (num[5] != 0) ? ((str != '') ? ' ' : '') + (a[Number(num[5])] || b[num[5][0]] + dash + a[num[5][1]]) : '';

   str = str.split("  ").join(' ');
   str = str.split("-thousand").join(" thousand");

 if(str[str.length-1] == " "){
   var sl = str.slice(0, str.length-1);
   return sl;
 }

  return str;

2 Comments

So what was the issue? And why not post a running version in a snippet?
I don't know how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.