Last active
April 4, 2022 07:31
-
-
Save MrMohebi/13bfc8650d29cecbe53f73c54a35d9a0 to your computer and use it in GitHub Desktop.
convert number in string to Persian string
This file contains 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
const numberToString = (str:string):string=>{ | |
let firstNum = (str.match(/\d+/) ?? [undefined])[0] | |
// couldn't find number | |
if(isNaN(+firstNum)) return str; | |
const num = parseInt(firstNum) | |
const stringNum = (num)=>{ | |
switch (num) { | |
case 0: return "صفر"; | |
case 1: return "یک"; | |
case 2: return "دو"; | |
case 3: return "سه"; | |
case 4: return "چهار"; | |
case 5: return "پنج"; | |
case 6: return "شش"; | |
case 7: return "هفت"; | |
case 8: return "هشت"; | |
case 9: return "نه"; | |
case 10: return "ده"; | |
case 11: return "یازده"; | |
case 12: return "دوازده"; | |
case 13: return "سیزده"; | |
case 14: return "چهارده"; | |
case 15: return "پانزده"; | |
case 16: return "شانزده"; | |
case 17: return "هفده"; | |
case 18: return "هجده"; | |
case 19: return "نانزده"; | |
case 20: return "بیست"; | |
case 30: return "سی"; | |
case 40: return "چهل"; | |
case 50: return "پنجاه"; | |
case 60: return "شصت"; | |
case 70: return "هفتاد"; | |
case 80: return "هشتاد"; | |
case 90: return "نود"; | |
} | |
} | |
if(num < 20){ | |
return str.replace(num.toString(),stringNum(num)) | |
} | |
if(num < 100){ | |
const unity = parseInt(num.toString()[1]); | |
const decimal = parseInt(num.toString()[0])*10; | |
return str.replace(num.toString(),stringNum(decimal) + " و " + stringNum(unity)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment