Search code examples
angularintegerarabicangular8arabic-support

Angular 8 How to read an Arabic characters number as a real integer to convert it into specific format using toLocaleString()


I need to convert english numbers (1234567890) to arabic:

 arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

Which I was successful in doing it using:

  let chars = value.toString(10).split('');
  //console.log(chars)
  for(let i=0; i<chars.length; i++)
  {
    chars[i]=arabicNumbers[chars[i]]
  }
  value = chars.join('');

The final value should formatted the same as the English number. For example, if I have a value of 89000000 USD after using toLocaleString('en-GB') it will be:

89,000,000

But in arabic it stays :

٨٩۰۰۰۰۰۰

What I need is:

٨٩,۰۰۰,۰۰۰

I tried charCodeAt() but it reconvert it to English characters instead


Solution

  • no need to convert chars to Arabic numbers just use:

    let enNumber = 89000000;
    console.log(enNumber.toLocaleString('fa-IR')); // ۸۹٬۰۰۰٬۰۰۰