1

Below is my code, when I try to convert 23, it converted to عشرين تلاته, but I want it converted to ثلاثة وعشرون وهكذا.

var th = ['', 'ألف', 'مليون', 'مليار', 'تريليون'];
        var dg = ['صفر', 'واحد', 'اثنين', 'ثلاثة', 'أربعة', 'خمسة', 'ستة', 'سبعة', 'ثمانية', 'تسعة'];
        var tn = ['عشرة', 'أحد عشر', 'اثني عشر', 'ثلاثة عشر', 'أربعة عشر', 'خمسة عشر', 'ستة عشر', 'سبعة عشر', 'ثمانية عشر', 'تسعة عشر'];
        var tw = ['عشرون', 'ثلاثون', 'الأربعين', 'خمسين', 'ستين', 'السبعين', 'ثمانين', 'تسعين'];

        function toWords(s) {
            s = s.toString();
            s = s.replace(/[\, ]/g, '');
            if (s != parseFloat(s)) return 'لیس عدد';
            var x = s.indexOf('.');
            if (x == -1) x = s.length;
            if (x > 15) return 'كبير جدا';
            var n = s.split('');
            var str = '';
            var sk = 0;
            for (var i = 0; i < x; i++) {
                if ((x - i) % 3 == 2) {
                    if (n[i] == '1') {
                        str += tn[Number(n[i + 1])] + ' ';
                        i++;
                        sk = 1;
                    } else if (n[i] != 0) {
                        str += tw[n[i] - 2] + ' ';
                        sk = 1;
                    }
                } else if (n[i] != 0) {
                    str += dg[n[i]] + ' ';
                    if ((x - i) % 3 == 0) str += 'مائة ';
                    sk = 1;
                }
                if ((x - i) % 3 == 1) {
                    if (sk) str += th[(x - i - 1) / 3] + ' ';
                    sk = 0;
                }
            }
            if (x != s.length) {
                var y = s.length;
                str += 'نقطة ';
                for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ';
            }
            return str.replace(/\s+/g, ' ');
        }
        
        
        
        console.log(toWords(23))
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
Emy
  • 19
  • 3
  • Is it possible to describe the expected behaviour in a few sentences? Its hard to understand the code whithout knowing what it should do. – Jonas Wilms Aug 05 '17 at 16:23
  • 120 -> مائة وعشرون بدلا من واحد مائة عشرون – Emy Aug 05 '17 at 16:26
  • هكذا doesn't exist in your code, how would it get in the result? – kpie Aug 05 '17 at 16:36
  • @kpie because its translated to Arabic from the original in this question : https://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript – wahdan Aug 05 '17 at 16:44
  • @Emy Have you also seen [this solution](https://stackoverflow.com/a/43418273/4003419) ? – LukStorms Aug 05 '17 at 18:55
  • @Emy please try this short function that works properly: https://stackoverflow.com/questions/57606651/convert-english-numbers-to-arabic-text-using-javascript/71369188#71369188 – Mohsen Alyafei Mar 06 '22 at 10:34

2 Answers2

1

var th = ['', 'ألف', 'مليون', 'مليار', 'تريليون'];
var dg = ['صفر', 'واحد', 'اثنين', 'ثلاثة', 'أربعة', 'خمسة', 'ستة', 'سبعة', 'ثمانية', 'تسعة'];
var tn = ['عشرة', 'أحد عشر', 'اثني عشر', 'ثلاثة عشر', 'أربعة عشر', 'خمسة عشر', 'ستة عشر', 'سبعة عشر', 'ثمانية عشر', 'تسعة عشر'];
var tw = ['عشرون', 'ثلاثون', 'الأربعين', 'خمسين', 'ستين', 'السبعين', 'ثمانين', 'تسعين'];

//console.log(th);
function toWords(s) {
 s = s.toString();
 s = s.replace(/[\, ]/g,'');
 if (s != parseFloat(s)) return 'ليس رقم';
 var x = s.indexOf('.');
 if (x == -1)
  x = s.length;
 if (x > 15)
  return 'too big';
 var n = s.split(''); 
 var str = '';
 var sk = 0;
 for (var i=0;   i < x;  i++) {
  if ((x-i)%3==2) { 
   if (n[i] == '1') {
    str += tn[Number(n[i+1])] + ' '+ ' ';
    i++;
    sk=1;
   } else if (n[i]!=0) {
    str += tw[n[i]-2] + ' ';
    sk=1;
   }
  } else if (n[i]!=0) { // 0235
   str = dg[n[i]] + ' ' + str + ' ';
   if ((x-i)%3==0) str += 'مائة و ';
   sk=1;
  }
  if ((x-i)%3==1) {
   if (sk)
    str += th[(x-i-1)/3] + ' ';
   sk=0;
  }
 }

 if (x != s.length) {
  var y = s.length;
  str += 'point ';
  for (var i=x+1; i<y; i++)
   str += dg[n[i]] +' ';
 }
 return str.replace(/\s+/g,' ');
}

console.log("23 : "+ toWords(23));
console.log("120: "+ toWords(120));
LukStorms
  • 28,916
  • 5
  • 31
  • 45
  • I updated my answer, but keep in mind that when you count in English you say **Once hundred Twenty** and in Arabic, it will be **واحد مائة وعشرون ** – Ahmed Abdulrahman Aug 05 '17 at 17:16
  • The number 2002 is outputted as `اثنين اثنين ألف` which not proper Arabic language. It is supposed to be: `ألفان واثنان` in Arabic. Also, 202 is outputted as `اثنين اثنين مائة و` and it supposed to be `مائتان واثنان`. – Mohsen Alyafei Mar 06 '22 at 10:33
  • Please try this short function that works properly: https://stackoverflow.com/questions/57606651/convert-english-numbers-to-arabic-text-using-javascript/71369188#71369188 – Mohsen Alyafei Mar 06 '22 at 10:35
0

Hi I don't understand Arabic but I guess the solution to this can be find in this particular question/solution. Convert digits into words with JavaScript

Samson Iyanda
  • 512
  • 3
  • 13
  • The OP is looking for an Arabic version. Converting to Arabic text is much more difficult as the text output depends on the type of subject counted if male or female subject. – Mohsen Alyafei Mar 06 '22 at 10:28