Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Arabic numbers to words with javascript

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

  • http://www.codeproject.com/Articles/112949/Number-To-Word-Arabic-Version
  • http://libstation.com/products/numbers-in-words/

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

like image 866
Alireza Fattahi Avatar asked Jul 06 '14 09:07

Alireza Fattahi


3 Answers

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.

like image 157
mmahgoub Avatar answered Nov 05 '22 21:11

mmahgoub


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.

like image 24
mmdts Avatar answered Nov 05 '22 22:11

mmdts


March 2022 - Function to Convert Currency Values to Arabic Full Words with Options

The Javascript function provided below converts Currency Values into Full Arabic Words taking into account the correct Arabic Grammer Rules for the counted subject (Masculine or Feminine) and the correct positioning of the Arabic string for the various types of numbers.

The function has built-in tanween to directly add tanween to the required words of numbers and currency names/country names as necessary.

Examples of Incorrect Arabic Grammer found in many conversion codes:

ثمان ليرات            ==> correct:   ثماني ليرات

خمسة عشر دينار كويتي   ==> correct:   خمسة عشر دينارًا كويتيًا 

مئتان جنيه            ==> correct:   مائتا جنيه

واحد وعشرون ليرة      ==> correct:   إحدى وعشرون ليرة

خمس آلاف ليرة        ==> correct:   خمسة آلاف ليرة

ألفان جنيه            ==> correct:   ألفا جنيه

واحد جنيه            ==> correct:   جنيه واحد

واحد ليرة            ==> correct:   ليرة واحدة

احدى عشر ليرة        ==> correct:   إحدى عشرة ليرة

اثنان جنيه            ==> correct:   جنيهان اثنان

The function also handles fractional parts of the value.

In its basic form, the function takes the number to be converted and the currency 3-letter ISO code. See below how to add more ISO codes.

Examples:

console.log(tafqeet( 2234.46 , "USD") );
console.log(tafqeet( 2234.46 , "LBP") );
console.log(tafqeet( 2234.46 , "EGP") );
console.log(tafqeet( 2234.46 , "KWD") );
console.log(tafqeet( 2234.46 , "SDG") );
console.log(tafqeet( 2234.46 , "JOD") );

// output:

ألفان ومائتان وأربعة وثلاثون دولارًا أمريكيًا، وستة وأربعون سنتًا
ألفان ومائتان وأربع وثلاثون ليرةً لبنانيةً، وستة وأربعون قرشًا
ألفان ومائتان وأربعة وثلاثون جنيهًا مصريًا، وستة وأربعون قرشًا
ألفان ومائتان وأربعة وثلاثون دينارًا كويتيًا، وأربعمائة وستون فلسًا
ألفان ومائتان وأربعة وثلاثون جنيهًا سودانيًا، وستة وأربعون قرشًا
ألفان ومائتان وأربعة وثلاثون دينارًا أردنيًا، وأربعمائة وستون فلسًا

Short Format Option

In the short format, the fractional part will be displayed as a fraction.

Examples:

console.log(tafqeet( 2234.46 , "USD", {format:'short'}) );
console.log(tafqeet( 2234.46 , "LBP", {format:'short'}) );
console.log(tafqeet( 2234.46 , "EGP", {format:'short'}) );
console.log(tafqeet( 2234.46 , "KWD", {format:'short'}) );
console.log(tafqeet( 2234.46 , "SDG", {format:'short'}) );
console.log(tafqeet( 2234.46 , "JOD", {format:'short'}) );

// output:

ألفان ومائتان وأربعة وثلاثون دولارًا أمريكيًا، و46/100 دولار أمريكي
ألفان ومائتان وأربع وثلاثون ليرةً لبنانيةً، و46/100 ليرة لبنانية
ألفان ومائتان وأربعة وثلاثون جنيهًا مصريًا، و46/100 جنيه مصري
ألفان ومائتان وأربعة وثلاثون دينارًا كويتيًا، و460/1000 دينار كويتي
ألفان ومائتان وأربعة وثلاثون جنيهًا سودانيًا، و46/100 جنيه سوداني
ألفان ومائتان وأربعة وثلاثون دينارًا أردنيًا، و460/1000 دينار أردني

Comma Option

When enabled, this option adds a comma between the output string triplets to separate the words for millions from that of thousands, etc. for better readability.

Legal Mode

This option is very critical when the output is used for writing cheques as it removes any ambiguity in the interpretation of the written words. This ambiguity does not occur when using the English language.

Compare the two (2) following output for the number 202000:

console.log(tafqeet( 202000 , "EGP") );
console.log(tafqeet( 202000 , "EGP", {legal:'on'}) );

// output:

مائتان وألفا جنيه مصري
مائتا ألف وألفا جنيه مصري

In the first case, without the legal options 'on', the output words could also mean 200+2000 which equals 2200. This is not what we intended. However, with the second case, with the option 'on', completely removes this ambiguity.

Adding Additional Currencies

Additional Currencies can be easily added to the list of the current 22 currencies at the end of the function.

Iso_Code:{                  // Currency ISO Code e.g. "USD"
uSingle :"",                // Unit Singluar Arabic Name e.g. "دولار"
uDouble:"",                 // Unit Double Arbic Name e.g. "دولاران"
uPlural:"",                 // Unit Plural Arbic Name e.g. "دولارات"
uGender:"male" or "female", // Unit Gender e.g. "male"
sSingle :"",                // Sub-Unit Singluar Arabic Name e.g. "سنت"
sDouble:"",                 // Sub-Unit Double Arbic Name e.g. "سنتان"
sPlural:"",                 // Sub- Unit Plural Arbic Name e.g. "سنتات"
sGender:"male" or "female", // Sub-Unit Gender e.g. "male"
fraction: number            // The fraction of the Sub-Unit normally 2 or 3
},

If you need the function for only one currency then you can delete the other codes in the list.

Example test units are provided for testing.

/*********************************************************************
* @function      : tafqeet(Number, ISO_code, [{options}])
* @purpose       : Converts Currency Values to Full Arabic Words
* @version       : 2.00
* @author        : Mohsen Alyafei
* @date          : 04 March 2022
* @Licence       : MIT
* @param         : {Number} Numeric (required)
* @param         : {code} 3-letter ISO Currency Code
* @param         : [{options}] 3 Options passed as object {name:value} as follows:
*                  {comma:'on'}      : Insert comma between triplet words.
*                  {legal: 'on'}     : Uses legal mode
*                  {format: 'short'} : Uses fractions for any decimal part.
* @returns       : {string} The wordified number string in Arabic.
*
**********************************************************************/
function tafqeet(numIn=0, code, op={}){
let iso=tafqeetISOList[code];if(!iso)throw new Error("Currency code not in the list!");
let arr=(numIn+="").split((0.1).toLocaleString().substring(1,2)),
out=nToW(arr[0],iso.uGender=="female",op,[iso.uSingle,iso.uDouble,iso.uPlural]),
frc=arr[1]?(arr[1]+"000").substring(0,iso.fraction):0;
if (frc !=0){out+="، و"+(op.format=="short"?frc+"/1"+"0".repeat(+iso.fraction)+" "+iso.uSingle:
nToW(frc,iso.sGender=="female",op,[iso.sSingle,iso.sDouble,iso.sPlural]));}
return out;

function nToW(numIn=0,fm,{comma,legal}={},names){
if(numIn==0)return"صفر "+iso.uSingle;
let tS=[,"ألف","مليون","مليار","ترليون","كوادرليون","كوينتليون","سكستليون"],tM=["","واحد","اثنان","ثلاثة","أربعة","خمسة","ستة","سبعة","ثمانية","تسعة","عشرة"],tF=["","واحدة","اثنتان","ثلاث","أربع","خمس","ست","سبع","ثمان","تسع","عشر"],
num=(numIn+=""),tU=[...tM],t11=[...tM],out="",n99,SpWa=" و",miah="مائة",
last=~~(((numIn="0".repeat(numIn.length*2%3)+numIn).replace(/0+$/g,"").length+2)/3)-1;
t11[0]="عشر";t11[1]="أحد";t11[2]="اثنا";
numIn.match(/.{3}/g).forEach((n,i)=>+n&&(out+=do999(numIn.length/3-i-1,n,i==last),i!==last&&(out+=(comma=='on'?"،":"")+SpWa)));
let sub=" "+names[0],n=+(num+"").slice(-2);if(n>10)sub=" "+tanween(names[0]);else if(n>2)sub=" "+names[2];
else if(n>0)sub=names[n-1]+" "+(fm?tF[n]:tM[n]);
return out+sub;

function tanween(n,a=n.split` `,L=a.length-1){
const strTF=(str,l=str.slice(-1),o=str+"ًا")=>{return"ا"==l?o=str.slice(0,-1)+"ًا":"ة"==l&&(o=str+"ً"),o;};
for(let i=0;i<=L;i++)if(!i||i==L)a[i]=strTF(a[i]);return a.join` `;}

function do999(sPos,num,last){
let scale=tS[sPos],n100=~~(num/100),nU=(n99=num%100)%10,n10=~~(n99/10),w100="",w99="",e8=(nU==8&&fm&&!scale)?"ي":"";
if (fm&&!scale){[tU,t11,t11[0],t11[1],t11[2]]=[[...tF],[...tF],"عشرة","إحدى","اثنتا"];if(n99>20)tU[1]="إحدى";}
if(n100){if(n100>2)w100=tF[n100]+miah;else if(n100==1)w100=miah;else w100=miah.slice(0,-1)+(!n99||legal=="on"?"تا":"تان");}
if(n99>19)w99=tU[nU]+(nU?SpWa:"")+(n10==2?"عشر":tF[n10])+"ون";
else if(n99>10)w99=t11[nU]+e8+" "+t11[0];else if(n99>2)w99=tU[n99]+e8;let nW=w100+(n100 && n99?SpWa:"")+w99;
if(!scale)return nW;if(!n99)return nW+" "+scale;if(n99>2)return nW+" "+(n99>10?scale+(last?"":"ًا")
:(sPos<3?[,"آلاف","ملايين"][sPos]:tS[sPos]+"ات"));nW=(n100?w100+((legal=="on"&&n99<3)?" "+scale:"")+SpWa:"")+scale;
return(n99==1)?nW:nW+(last?"ا":"ان");}}}
//=====================================================================







//==================== Common ISO Currency List in Arabic ===============
let tafqeetISOList={
AED:{uSingle :"درهم إماراتي",uDouble:"درهمان إماراتيان",uPlural:"دراهم إماراتية",uGender:"male",
 sSingle :"فلس",sDouble:"فلسان",sPlural:"فلوس",sGender:"male",fraction:2},

BHD:{uSingle :"دينار بحريني",uDouble:"ديناران بحرينيان",uPlural:"دنانير بحرينية",uGender:"male",
 sSingle :"فلس",sDouble:"فلسان",sPlural:"فلوس",sGender:"male",fraction:3},

EGP:{uSingle :"جنيه مصري",uDouble:"جنيهان مصريان",uPlural:"جنيهات مصرية",uGender:"male",
 sSingle :"قرش",sDouble:"قرشان",sPlural:"قروش",sGender:"male",fraction:2},

EUR:{uSingle :"يورو",uDouble:"يوروان",uPlural:"يوروات",uGender:"male",
 sSingle:"سنت",sDouble:"سنتان",sPlural:"سنتات",sGender:"male",fraction:2},

GBP:{uSingle :"جنيه إسترليني",uDouble:"جنيهان إسترلينيان",uPlural:"جنيهات إسترلينية",uGender:"male",
 sSingle :"بنس",sDouble:"بنسان",sPlural:"بنسات",sGender:"male",fraction:2},

INR:{uSingle :"روبية هندية",uDouble:"روبيتان هنديتان",uPlural:"روبيات هندية",uGender:"female",
 sSingle :"بيسة",sDouble:"بيستان",sPlural:"بيسات",sGender:"female",fraction:2},

IQD:{uSingle :"دينار عراقي",uDouble:"ديناران عراقيان",uPlural:"دنانير عراقية",uGender:"male",
 sSingle :"فلس",sDouble:"فلسان",sPlural:"فلوس",sGender:"male",fraction:2},

JOD:{uSingle :"دينار أردني",uDouble:"ديناران أردنيان",uPlural:"دنانير أردنية",uGender:"male",
 sSingle :"فلس",sDouble:"فلسان",sPlural:"فلوس",sGender:"male",fraction:3},

KWD:{uSingle :"دينار كويتي",uDouble:"ديناران كويتيان",uPlural:"دنانير كويتية",uGender:"male",
 sSingle :"فلس",sDouble:"فلسان",sPlural:"فلوس",sGender:"male",fraction:3},

LBP:{uSingle :"ليرة لبنانية",uDouble:"ليرتان لبنانيتان",uPlural :"ليرات لبنانية",uGender:"female",
 sSingle :"قرش",sDouble:"قرشان",sPlural:"قروش",sGender:"male",fraction:2},

LYD:{uSingle :"دينار ليبي",uDouble:"ديناران ليبيان",uPlural:"دنانير ليبية",uGender:"male",
 sSingle:"درهم",sDouble:"درهمان",sPlural: "دراهم",sGender:"male",fraction:3},

MAD:{uSingle :"درهم مغربي",uDouble:"درهمان مغربيان",uPlural:"دراهم مغربية",uGender:"male",
 sSingle :"سنتيم",sDouble:"سنتيمان",sPlural:"سنتيمات",sGender:"male",fraction:2},

OMR:{uSingle :"ريال عماني",uDouble:"ريالان عمانيان",uPlural:"ريالات عمانية",uGender:"male",
 sSingle :"بيسة",sDouble:"بيستان",sPlural:"بيسات",sGender:"female",fraction:3},

QAR:{uSingle:"ريال قطري",uDouble:"ريالان قطريان",uPlural:"ريالات قطرية",uGender:"male",
 sSingle:"درهم",sDouble:"درهمان",sPlural: "دراهم",sGender:"male",fraction:2},

SAR:{uSingle:"ريال سعودي",uDouble:"ريالان سعوديان",uPlural:"ريالات سعودية",uGender:"male",
 sSingle:"هللة",sDouble:"هللتان",sPlural: "هللات",sGender:"female",fraction:2},

SDG:{uSingle :"جنيه سوداني",uDouble:"جنيهان سودانيان",uPlural:"جنيهات سودانية",uGender:"male",
 sSingle :"قرش",sDouble:"قرشان",sPlural:"قروش",sGender:"male",fraction:2},

SOS:{uSingle :"شلن صومالي",uDouble:"شلنان صوماليان",uPlural:"شلنات صومالية",uGender:"male",
 sSingle:"سنت",sDouble:"سنتان",sPlural:"سنتات",sGender:"male",fraction:2},

SSP:{uSingle :"جنيه جنوب سوداني",uDouble:"جنيهان جنوب سودانيان",uPlural:"جنيهات جنوب سودانية",uGender:"male",
 sSingle :"قرش",sDouble:"قرشان",sPlural:"قروش",sGender:"male",fraction:2},

SYP:{uSingle :"ليرة سورية",uDouble:"ليرتان سوريتان",uPlural :"ليرات سورية",uGender:"female",
 sSingle :"قرش",sDouble:"قرشان",sPlural:"قروش",sGender:"male",fraction:2},

TND:{uSingle :"دينار تونسي",uDouble:"ديناران تونسيان",uPlural:"دنانير تونسية",uGender:"male",
 sSingle :"مليم",sDouble:"مليمان",sPlural:"ملاليم",sGender:"male",fraction:3},

USD:{uSingle:"دولار أمريكي",uDouble:"دولاران أمريكيان",uPlural:"دولارات أمريكية",uGender:"male",
 sSingle:"سنت",sDouble:"سنتان",sPlural:"سنتات",sGender:"male",fraction:2},

YER:{uSingle:"ريال يمني",uDouble:"ريالان يمنيان",uPlural:"ريالات يمنية",uGender:"male",
 sSingle:"فلس",sDouble:"فلسان",sPlural: "فلوس",sGender:"male",fraction:2},

//==== add here

}; // end of list

//***********************************************










//========================
var r=0; // test tracker
//===============
r |= test(1,"QAR",{},"ريال قطري واحد");
r |= test(1.01,"QAR",{},"ريال قطري واحد، ودرهم واحد");
r |= test(0.02,"QAR",{},"صفر ريال قطري، ودرهمان اثنان");
r |= test(2.08,"QAR",{},"ريالان قطريان اثنان، وثمانية دراهم");
r |= test(2.03,"QAR",{},"ريالان قطريان اثنان، وثلاثة دراهم");
r |= test(2.25,"QAR",{},"ريالان قطريان اثنان، وخمسة وعشرون درهمًا");
r |= test(21.18,"QAR",{},"واحد وعشرون ريالًا قطريًا، وثمانية عشر درهمًا");
r |= test(221.21,"QAR",{},"مائتان وواحد وعشرون ريالًا قطريًا، وواحد وعشرون درهمًا");
r |= test(200.21,"QAR",{},"مائتا ريال قطري، وواحد وعشرون درهمًا");
r |= test(2000.15,"QAR",{},"ألفا ريال قطري، وخمسة عشر درهمًا");
r |= test(21022.38,"QAR",{},"واحد وعشرون ألفًا واثنان وعشرون ريالًا قطريًا، وثمانية وثلاثون درهمًا");
r |= test(200000.38,"QAR",{},"مائتا ألف ريال قطري، وثمانية وثلاثون درهمًا");
r |= test(2000000.123,"QAR",{},"مليونا ريال قطري، واثنا عشر درهمًا");

if (r==0) console.log("✅ Test Cases - Male Currency & Male Sub-Currency   ....Passed.");

//========================
r |= test(1,"LBP",{},"ليرة لبنانية واحدة");
r |= test(1.01,"LBP",{},"ليرة لبنانية واحدة، وقرش واحد");
r |= test(0.02,"LBP",{},"صفر ليرة لبنانية، وقرشان اثنان");
r |= test(2.08,"LBP",{},"ليرتان لبنانيتان اثنتان، وثمانية قروش");
r |= test(2.03,"LBP",{},"ليرتان لبنانيتان اثنتان، وثلاثة قروش");
r |= test(2.25,"LBP",{},"ليرتان لبنانيتان اثنتان، وخمسة وعشرون قرشًا");
r |= test(21.18,"LBP",{},"إحدى وعشرون ليرةً لبنانيةً، وثمانية عشر قرشًا");
r |= test(221.21,"LBP",{},"مائتان وإحدى وعشرون ليرةً لبنانيةً، وواحد وعشرون قرشًا");
r |= test(200.21,"LBP",{},"مائتا ليرة لبنانية، وواحد وعشرون قرشًا");
r |= test(2000.15,"LBP",{},"ألفا ليرة لبنانية، وخمسة عشر قرشًا");
r |= test(21022.38,"LBP",{},"واحد وعشرون ألفًا واثنتان وعشرون ليرةً لبنانيةً، وثمانية وثلاثون قرشًا");
r |= test(200000.38,"LBP",{},"مائتا ألف ليرة لبنانية، وثمانية وثلاثون قرشًا");
r |= test(2000000.123,"LBP",{},"مليونا ليرة لبنانية، واثنا عشر قرشًا");

if (r==0) console.log("✅ Test Cases - Female Currency & Male Sub-Currency   ....Passed.");

//========================
r |= test(1,"OMR",{},"ريال عماني واحد");
r |= test(1.01,"OMR",{},"ريال عماني واحد، وعشر بيسات");
r |= test(0.02,"OMR",{},"صفر ريال عماني، وعشرون بيسةً");
r |= test(2.08,"OMR",{},"ريالان عمانيان اثنان، وثمانون بيسةً");
r |= test(2.03,"OMR",{},"ريالان عمانيان اثنان، وثلاثون بيسةً");
r |= test(2.25,"OMR",{},"ريالان عمانيان اثنان، ومائتان وخمسون بيسةً");
r |= test(21.18,"OMR",{},"واحد وعشرون ريالًا عمانيًا، ومائة وثمانون بيسةً");
r |= test(221.21,"OMR",{},"مائتان وواحد وعشرون ريالًا عمانيًا، ومائتان وعشر بيسات");
r |= test(200.21,"OMR",{},"مائتا ريال عماني، ومائتان وعشر بيسات");
r |= test(2000.15,"OMR",{},"ألفا ريال عماني، ومائة وخمسون بيسةً");
r |= test(21022.38,"OMR",{},"واحد وعشرون ألفًا واثنان وعشرون ريالًا عمانيًا، وثلاثمائة وثمانون بيسةً");
r |= test(200000.38,"OMR",{},"مائتا ألف ريال عماني، وثلاثمائة وثمانون بيسةً");
r |= test(2000000.123,"OMR",{},"مليونا ريال عماني، ومائة وثلاث وعشرون بيسةً");

if (r==0) console.log("✅ Test Cases - Male Currency & Female Sub-Currency   ....Passed.");


//------------------
function test(n,code,options,should) {
let result = tafqeet(n,code,options);
if (result !== should) {console.log(`${n} Output   : ${result}\n${n} Should be: ${should}`);return 1;}
}
<input type="text"
    name="number"
    placeholder="ادخل الرقم"
    autocomplete="off"
    onkeyup="output.innerHTML=tafqeet(this.value,'SAR')" />

    <div id="output" style="font-weight:bold; font-size: 22px;"></div>
 
like image 6
Mohsen Alyafei Avatar answered Nov 05 '22 21:11

Mohsen Alyafei