PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

[FIXED] How to convert latin numbers to arabic in javascript?

 August 07, 2022     decimal, javascript, number-formatting, numbers, reactjs     No comments   

Issue

I use a custom function to convert numbers to arabic format like this :

 const numbers = `۰۱۲۳۴۵۶۷۸۹`;
  const convert = (num) => {
    let res = "";
    const str = num.toString();
    for (let c of str) {
      res += numbers.charAt(c);
    }
    return res;
  };

And it works like this :

console.log(convert(123)) // ==> ۱۲۳

The problem occurs when there is a number with decimals and it converts it to arabic format without the decimal dots for example :

console.log(convert(123.9)) // ==> ۱۲۳۹

I expect the output to be ۱۲۳،۹ .

How can I convert numbers with decimals to arabic format with decimal dots included with my function ?


Solution

Try toLocaleString()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

console.log((123.9).toLocaleString('ar-AE'))

Edit:

with toLocaleString options

(123.9872512).toLocaleString("ar-AE", {
  useGrouping: false,
  maximumSignificantDigits: 10
})


Answered By - User863
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing