如何在javascript中将拉丁数字转换为阿拉伯数字?

我使用自定义函数将数字转换为阿拉伯语格式,如下所示:

!pip install upsetplot==0.4.1

它是这样工作的:

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

当有一个带小数的数字并且将其转换为没有小数点的阿拉伯格式时会出现问题,例如:

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

我希望输出为 console.log(convert(123.9)) // ==> ۱۲۳۹ .

如何将带小数的数字转换为我的函数中包含小数点的阿拉伯格式?

huang118118bing 回答:如何在javascript中将拉丁数字转换为阿拉伯数字?

试试toLocaleString()

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

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

编辑:

带有 toLocaleString 个选项

(123.9872512).toLocaleString("ar-AE",{
  useGrouping: false,maximumSignificantDigits: 10
})
本文链接:https://www.f2er.com/77031.html

大家都在问