Convert Arabic numbers to words with javascript

2019-07-19 07:31发布

I am looking for JavaScript function to convert numbers to Arabic words

For example

  • 23 > ثلاثة وعشرين
  • 53 > ثلاثة وخمسون
  • .... > ....

I found some solutions but it is all .net

I have searched the web but could not find any solution ( well, i could not understand while searching in Arabic ;) )

3条回答
Viruses.
2楼-- · 2019-07-19 07:53

I've written an NPM package for this check: https://github.com/mmahgoub/tafgeetjs

$npm install tafgeetjs

Usage: var Tafgeet = require('tafgeetjs');

var stringText = new Tafgeet(556563.20, 'SDG').parse();

this will produce: 'فقط خمسمائة وستة وخمسون ألف وخمسمائة وثلاثة وستون جنيه سوداني وعشرون قرش لا غير'.

There is an Angular example too.

查看更多
成全新的幸福
3楼-- · 2019-07-19 08:04

This answer is two years late, but...

All the answers to this question assume Arabic number words work the same ways as English ones do. Unfortunately, this is not true. Arabic sentence structure is much, much more complex than English ones, and the words used for numerals vary based on whether what you're counting is a masculine object or a feminine object (objects in Arabic, like in French, have gender), and based on the position of the counted object in the sentence (whether it is the subject, the object, after a preposition, etc).

For masculine objects (مذكر) that are مرفوعين, you can use this snippet that I just wrote because I couldn't find the answer to this question anywhere. It works for numbers less than 1 million, and does not work for what's after the floating point (yet).

var P = {
  l : ['صفر', ' ألف'],
  unis : ['', 'واحد', 'اثنين', 'ثلاثة', 'أربعة', 'خمسة', 'ستة', 'سبعة', 'ثمانية', 'تسعة'],
  tens : ['', 'عشرة', 'عشرون', 'ثلاثون', 'أربعون', 'خمسون', 'ستون', 'سبعون', 'ثمانون', 'تسعون'],
 xtens : ['عشرة', 'أحد عشر', 'اثنا عشر', 'ثلاثة عشر', 'أربعة عشر', 'خمسة عشر', 'ستة عشر', 'سبعة عشر', 'ثمانية عشر', 'تسعة عشر'],
  huns : ['', 'مائة', 'مئتان', 'ثلاث مائة', 'اربع مائة', 'خمس مائة', 'ست مائة', 'سبع مائة', 'ثمان مائة', 'تسع مائة'],
  thos : ['', 'ألف', 'ألفان', 'ثلاثة ألاف', 'اربعة ألاف', 'خمسة ألاف', 'ستة ألاف', 'سبعة ألاف', 'ثمانية ألاف', 'تسعة ألاف'],
 xthos : ['عشرة ألاف', 'أحد عشر ألف', 'اثنا عشر ألف', 'ثلاثة عشر ألف', 'أربعة عشر ألف', 'خمسة عشر ألف', 'ستة عشر ألف', 'سبعة عشر ألف', 'ثمانية عشر ألف', 'تسعة عشر ألف'],
   and : 'و',
};
var N = '٠١٢٣٤٥٦٧٨٩';
function replaceNumerals(s){
  if(!(/[٠١٢٣٤٥٦٧٨٩]+/).test(s))return s;
  let t = typeof(s);
  s = s.split('').map((o)=>{
    if(N.indexOf(o) != -1)
      return N.indexOf(o);
    return o;
  }).join('');
  return t=="number"?~~s:s;
}
function pounds(y) {
    y = replaceNumerals(y);
    s = y.toString().replace(/[\, ]/g, '');
    if (s != parseFloat(s)) return false;
    var x = s.indexOf('.');x = x==-1?s.length:x;
    if (x > 6 || s.length - x > 2) return false;
    y = parseFloat(s);
    d = y - ~~y;
    y = ~~y;
    if(!y)return P.l[0];
    let str = [], r, v = 0, p, c = ~~y%10, n, i = 1;n = (r=~~(y/Math.pow(10,i++)))?r%10:undefined;
    do {
      //Units
      if(c > 0)str.push(P.unis[c]);
      if(n===undefined)break;p = c;c = n;n = (r=~~(y/Math.pow(10,i++)))?r%10:undefined;v += p*Math.pow(10,i-3);
      //Tens
      if(c == 1)str[0] = P.xtens[p];
      if(c > 1){
        if(v > 0)str.unshift(P.and);
        str.unshift(P.tens[c]);
      }
      if(n===undefined)break;p = c;c = n;n = (r=~~(y/Math.pow(10,i++)))?r%10:undefined;v += p*Math.pow(10,i-3);
      //Hundreds
      if(v > 0 && c > 0)str.push(P.and);
      if(c > 0)str.push(P.huns[c]);
      if(n===undefined)break;p = c;c = n;n = (r=~~(y/Math.pow(10,i++)))?r%10:undefined;v += p*Math.pow(10,i-3);
      //Thousands
      if(v > 0 && c > 0 && !n)str.push(P.and);
      if(c > 0 && !n)str.push(P.thos[c]);
      if(n===undefined)break;p = c;c = n;n = (r=~~(y/Math.pow(10,i++)))?r%10:undefined;v += p*Math.pow(10,i-3);
      //Ten Thousands
      if(v > 0 && c > 0 && y - c*1e4 - p*1e3 > 0)str.push(P.and);
      if(c == 1)str.push(P.xthos[p]);
      if(c > 1){
        str.push(P.l[1]);
        str.push(P.tens[c]);
        if(p > 0){
          str.push(P.and);
          str.push(P.unis[p]);
        }
      }
      if(n===undefined)break;p += 10*c;c = n;n = (r=~~(y/Math.pow(10,i++)))?r%10:undefined;v += p*Math.pow(10,i-3);
      //Hundred Thousands
      if(v > 0 && c > 0)str.push(P.and);
      if(c > 0){
        if(!p)str.push(P.l[1]);
        str.push(P.huns[c]);
      }
    } while(false);
    return str.reverse().join(' ');
}

It could get more refined than this, but, considering how the rules of the language are not as uniform as English, doing this with a loop (like you would in English) would be very counter-productive in my opinion.

查看更多
\"骚年 ilove
4楼-- · 2019-07-19 08:05

You can do use similar function :

http://jsbin.com/jesoj/1/

Zero to Million :

function zeroToMillion(num) {
    num = (num + "").replace(" ", "");
    var exceptional = {0: "zero", 11: "eleven", 12: "twelve",
                       13: "thirteen", 14: "fourteen", 15: "fifteen",
                       16: "sixteen", 17: "seventeen", 18: "eighteen",
                       19: "nineteen"};

    var digit = ["", "one", "two", "three", "four", "five", "six",
                 "seven", "eight", "nine"]; // don't add zero
    var decade = ["", "ten", "twenty", "thirty", "forty", "fifty",
                  "sixty", "seventy", "eighty", "ninety"]; // don't add zero

    var largenumber = ["hundred", "thousand", "million", "billion"];

    var l = num.length-1,
        a = (num[l]) ? num[l] : 0,
        b = (num[l-1]) ? num[l-1] : 0,
        ba = ""+b+a,
        c = (num[l-2]) ? num[l-2] : 0,
        d = (num[l-3]) ? num[l-3] : 0,
        e = (num[l-4]) ? num[l-4] : 0,
        ed = ""+e+d,
        f = (num[l-5]) ? num[l-5] : 0,
        num_ab = (exceptional[ba]) ? exceptional[ba] : (decade[b]+" "+digit[a]),
        num_ed = (exceptional[ed]) ? exceptional[ed] : (decade[e]+" "+digit[d]),
        name_c = (c!==0) ? (digit[c]+" "+largenumber[0])+" " : "",
        name_d = (num_ed!==" ") ? (num_ed+" "+largenumber[1])+" " : "",
        name_f = (f!==0) ? (digit[f]+" "+largenumber[0])+" " : "";
    return name_f+name_d+name_c+num_ab;
}

Zero to one hundred :

function zeroToHundred(num) {
    num = num+""; // .toString();
    var exceptional = {0: "zero", 11: "eleven", 12: "twelve",
                       13: "thirteen", 14: "fourteen", 15: "fifteen",
                       16: "sixteen", 17: "seventeen", 18: "eighteen",
                       19: "nineteen", 100: "one hundred"};

    var digit = ["", "one", "two", "three", "four", "five", "six",
                 "seven", "eight", "nine"]; // don't add zero
    var decade = ["", "ten", "twenty", "thirty", "forty", "fifty",
                  "sixty", "seventy", "eighty", "ninety"]; // don't add zero

    if (exceptional[num]) {
        return exceptional[num];
    } else {
        var b = (num[1]) ? num[1] : num[0],
            a = (num[1]) ? num[0] : 0;
        return decade[a]+" "+digit[b];
    }
}

Good translate.

查看更多
登录 后发表回答