Last active
April 24, 2024 12:04
-
-
Save k-msalehi/803f72b5e5009cfad30a3bb33e5a50af to your computer and use it in GitHub Desktop.
Convert English digits to persian digits or persian words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// with thousand separator | |
const convertToPersianDigits = (number) => number.toLocaleString('fa-IR') | |
// without thousand separator | |
const convertToPersianDigitsNoDec = (string) => string.toLocaleString('fa-IR', {useGrouping: false}) | |
// Keep non numeric characters intact and convert english digits to persian | |
function convertToPersianDigits(input) { | |
// Convert input to a string | |
input = input.toString(); | |
// Define arrays for English and Persian digits | |
const englishDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; | |
const persianDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; | |
// Convert each character in the input string | |
const result = input.split('').map(char => { | |
// Check if the character is an English digit | |
if (englishDigits.includes(char)) { | |
// Replace English digit with its Persian counterpart | |
return persianDigits[englishDigits.indexOf(char)]; | |
} else { | |
// Keep non-numeric characters intact | |
return char; | |
} | |
}); | |
// Join the characters back into a string and return | |
return result.join(''); | |
} | |
// remove none numeric characters and convert to persian digits | |
function toFarsiNumber(n) { | |
const farsiDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; | |
return n | |
.toString() | |
.split('') | |
.map(x => farsiDigits[x]) | |
.join(''); | |
} | |
// english digits to persian string (wordify number) | |
function enNumToFaWords(str) { | |
var delimiter, digit, i, iThree, numbers, parts, result, resultThree, three; | |
if (!isFinite(str)) { | |
return ""; | |
} | |
if (typeof str !== "string") { | |
str = str.toString(); | |
} | |
parts = ["", "هزار", "میلیون", "میلیارد", "هزار میلیارد", "کوادریلیون", "کویینتیلیون", "سکستیلیون"]; | |
if (str.toString().length >= 13 && str.toString().length <= 15) { | |
numberSlices = str.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
numberSlices = numberSlices.split(","); | |
sliceToCheck = numberSlices[1]; | |
if (sliceToCheck !== "000") { | |
parts = ["", "هزار", "میلیون", "میلیارد", "هزار", "کوادریلیون", "کویینتیلیون", "سکستیلیون"]; | |
} | |
} | |
numbers = { | |
0: ["", "صد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد"], | |
1: ["", "ده", "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود"], | |
2: ["", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه"], | |
two: ["ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده"], | |
zero: "صفر", | |
}; | |
delimiter = " و "; | |
// debugger; | |
str = str | |
.split("") | |
.reverse() | |
.join("") | |
.replace(/\d{3}(?=\d)/g, "$&,") | |
.split("") | |
.reverse() | |
.join("") | |
.split(",") | |
.map(function (str) { | |
return Array(4 - str.length).join("0") + str; | |
}); | |
result = (function () { | |
var results; | |
results = []; | |
for (iThree in str) { | |
three = str[iThree]; | |
resultThree = (function () { | |
var j, len, results1; | |
results1 = []; | |
for (i = j = 0, len = three.length; j < len; i = ++j) { | |
digit = three[i]; | |
if (i === 1 && digit === "1") { | |
results1.push(numbers.two[three[2]]); | |
} else if ((i !== 2 || three[1] !== "1") && numbers[i][digit] !== "") { | |
results1.push(numbers[i][digit]); | |
} else { | |
continue; | |
} | |
} | |
return results1; | |
})(); | |
resultThree = resultThree.join(delimiter); | |
results.push(resultThree + " " + parts[str.length - iThree - 1]); | |
} | |
return results; | |
})(); | |
result = result.filter(function (x) { | |
return x.trim() !== ""; | |
}); | |
//ali | |
var copystr = str; | |
copystr.splice(str.length - 1, 1); | |
var copyresult = result; | |
//find 000 elements | |
//must delete indexes | |
var indexes = []; | |
for (var i = 0; i < copystr.length; i++) { | |
if (copystr[i] == "000") { | |
indexes.push(i); | |
} | |
} | |
//set elementsto -1 for delete later | |
// debugger; | |
for (var i = 0; i < indexes.length; i++) { | |
result[indexes[i]] = "-1"; | |
} | |
var length = result.length; | |
while (length--) { | |
if (result[length] == "-1") { | |
result.splice(length, 1); | |
} | |
} | |
//end ali | |
result = result.join(delimiter).trim(); | |
if (result !== "") { | |
return result; | |
} else { | |
return numbers.zero; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment