Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript number to words

var num = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" ");
var tens = "twenty thirty forty fifty sixty seventy eighty ninety".split(" ");

function number2words(n){
    if (n < 20) return num[n];
    var digit = n%10;
    if (n < 100) return tens[~~(n/10)-2] + (digit? "-" + num[digit]: "");
    if (n < 1000) return num[~~(n/100)] +" hundred" + (n%100 == 0? "": " and " + number2words(n%100));
    return number2words(~~(n/1000)) + " thousand" + (n%1000 != 0? " " + number2words(n%1000): "");
}
Comment

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 :: how to launch several async functions in node js 
Javascript :: remove special characters from string 
Javascript :: jquery find div with data attribute value 
Javascript :: typescript read json file 
Javascript :: javascript style font size 
Javascript :: get hash js 
Javascript :: how to remove a list of classes from an element using js 
Javascript :: using bootstrap in react 
Javascript :: simple javascript code 
Javascript :: What is the Difference between Undefined, undeclared, Null 
Javascript :: delete local storage javascript 
Javascript :: react add inline styles in react 
Javascript :: discord.js clean content 
Javascript :: javascript compare arrays 
Javascript :: sequelize where more than 
Javascript :: nodejs request 
Javascript :: include cookies in fetch 
Javascript :: import a script to my react componetn 
Javascript :: js get element by X Y 
Javascript :: jquery append div 
Javascript :: check if string contains lowercase javascript 
Javascript :: angular get current route 
Javascript :: cypress verify url 
Javascript :: Too long with no output (exceeded 10m0s): context deadline exceeded 
Javascript :: map javascript index 
Javascript :: javascript get current day of month 
Javascript :: $(window).scrolltop() not working on mobile 
Javascript :: basic express graphql 
Javascript :: reset input react 
Javascript :: how to iterate over list in jquery 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =