Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert number to word js

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",
];

function inWords(num) {
  if ((num = num.toString()).length > 9) return "overflow";
  let 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]]) + "crore "
      : "";
  str +=
    n[2] != 0
      ? (a[Number(n[2])] || b[n[2][0]] + " " + a[n[2][1]]) + "lakh "
      : "";
  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;
}

/* eslint eqeqeq: 0 */
Comment

PREVIOUS NEXT
Code Example
Javascript :: Nodemailer gmail new configuration 
Javascript :: express routing 
Javascript :: create multiple collections in mongodb 
Javascript :: express response setTimeout 
Javascript :: javaSript string first words to upper case 
Javascript :: nidejs aws sdk s3 copy 
Javascript :: javascript copy image to clipboard 
Javascript :: javascript get device width 
Javascript :: parse date from string in js 
Javascript :: difference between statement and expression 
Javascript :: jquery in array 
Javascript :: inline style react 
Javascript :: epsilon javascript 
Javascript :: javascript check if null 
Javascript :: get date in javascript 
Javascript :: html table to excel javascript 
Javascript :: regex exact match case insensitive 
Javascript :: add multiple event listeners 
Javascript :: firestore set a document 
Javascript :: javascript regex generator 
Javascript :: express js static files 
Javascript :: link stylesheet in javascript 
Javascript :: react native floating button 
Javascript :: detect if two line segments intersect each other javascript 
Javascript :: wordpress jquery slide out navigation 
Javascript :: history push search params 
Javascript :: import modules js html 
Javascript :: react string to integer 
Javascript :: react pagination 
Javascript :: url regular expression 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =