Skip to content

Instantly share code, notes, and snippets.

@intuxicated
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intuxicated/62d86b1c43f265bd36b6 to your computer and use it in GitHub Desktop.
Save intuxicated/62d86b1c43f265bd36b6 to your computer and use it in GitHub Desktop.
convert english numbers to persian number
function pnumber(text){
var en_to_persian = {"1":"۱", "2":"۲","3":"۳","4":"۴","5":"۵","6":"۶","7":"۷","8":"۸","9":"۹","0":"۰"}
var ar_to_persian = {"١":"۱", "٢":"۲","٣":"۳","٤":"۴","٥":"۵","٦":"۶","٧":"۷","٨":"۸","٩":"۹","٠":"۰"}
for (var val in en_to_persian)
text = text.replace(new RegExp(val, "g"), en_to_persian[val]);
for (var val in ar_to_persian)
text = text.replace(new RegExp(val, "g"), ar_to_persian[val]);
return text
}
@Bersam
Copy link

Bersam commented Jul 6, 2014

Why you do it in two for loop? i think you can do it in just one loop!
var ar_and_en_to_persian = {"1":"۱", "2":"۲","3":"۳","4":"۴","5":"۵","6":"۶","7":"۷","8":"۸","9":"۹","0":"۰","١":"۱", "٢":"۲","٣":"۳","٤":"۴","٥":"۵","٦":"۶","٧":"۷","٨":"۸","٩":"۹","٠":"۰"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment