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 :: javascript random char 
Javascript :: scroll to top in react 
Javascript :: how to insert image by javascript 
Javascript :: check data in formData 
Javascript :: ajax add header 
Javascript :: jquery submit refresh page stop 
Javascript :: conditionally set checkbox state in React 
Javascript :: remove duplicate value from map react js 
Javascript :: how to check array is sorted or not in javascript 
Javascript :: jquery bind function to multiple events 
Javascript :: get time from a date time in jquery 
Javascript :: vetur Property has no initializer and is not definitely assigned in 
Javascript :: change logo sapui5 
Javascript :: native base expo web eror 
Javascript :: clean up async requests in react useEffect hook using abort controller 
Javascript :: javascript video after play 
Javascript :: use history react router dom 
Javascript :: jquery select child span 
Javascript :: how to set header in angular 8post 
Javascript :: Javascript remove array item by value 
Javascript :: check if reCaptcha is sucess 
Javascript :: ljs date get minor date 
Javascript :: javascript get a random number with 6 digits 
Javascript :: remove parent tr jquery 
Javascript :: localstorage setitem javascript 
Javascript :: float js precision 
Javascript :: jquery wysiwyg editor val acf 
Javascript :: messageReactionAdd 
Javascript :: width 100% react native 
Javascript :: javascript find object by property in array 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =