I'm using const to define changes and took me so long. How can I use them and how can I brief the codes?:
const [num0en, num1en, num2en, num3en, num4en, num5en, num6en, num7en, num8en, num9en, percEn] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "%"]
const [num0fa, num1fa, num2fa, num3fa, num4fa, num5fa, num6fa, num7fa, num8fa, num9fa, PercFa] = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "٪"]
const En0 = "0";
const Pe0 = "۰";
const En1 = '1';
const Pe1 = '۱';
const En2 = "2";
const Pe2 = "۲";
const En3 = "3";
const Pe3 = "۳";
const En4 = "4";
const Pe4 = "۴";
const En5 = "5";
const Pe5 = "۵";
const En6 = "6";
const Pe6 = "۶";
const En7 = "7";
const Pe7 = "۷";
const En8 = "8";
const Pe8 = "۸";
const En9 = "9";
const Pe9 = "۹";
const EnPercents = "%";
const PePercents = "٪";
const Rep0 = num0en.replaceAll(En0, Pe0);
const Rep1 = num1en.replaceAll(En1, Pe1);
const Rep2 = num2en.replaceAll(En2, Pe2);
const Rep3 = num3en.replaceAll(En3, Pe3);
const Rep4 = num4en.replaceAll(En4, Pe4);
const Rep5 = num5en.replaceAll(En5, Pe5);
const Rep6 = num6en.replaceAll(En6, Pe6);
const Rep7 = num7en.replaceAll(En7, Pe7);
const Rep8 = num8en.replaceAll(En8, Pe8);
const Rep9 = num9en.replaceAll(En9, Pe9);
const RepPer = percEn.replaceAll(EnPercents, PePercents);
const ReplaceAllNumbers = Rep0&&Rep1&&Rep2&&Rep3&&Rep4&&Rep5&&Rep6&&Rep7&&Rep8&&Rep9&&RepPer
var post = "0123456789";
// I need to use it in post below:
document.write(post);
and by the way, you would notice some mistakes. I beg you pardon beforehand..
CodePudding user response:
var locale = "fa-IR"
let num = 1234567890;
document.getElementById("num").innerText = num.toLocaleString(locale, {
style: "decimal",
useGrouping: false
});
num = .42;
document.getElementById("pct").innerText = num.toLocaleString(locale, {
style: "percent",
useGrouping: true
});
num = 1027;
document.getElementById("cur").innerText = num.toLocaleString(locale, {
style: "currency",
currency: "EUR"
});
<p id="num"></p>
<p id="pct"></p>
<p id="cur"></p>
CodePudding user response:
Use a regular expression, and use a function as the replacement so it can return the corresponding replacement. This correspondence can be in an object.
const enToPe = {
[num0en]: num0fa,
[num1en]: num1fa,
..
};
let result = post.replace(/./g, char => enToPe[char] || char);
console.log(result);