Skip to content

Instantly share code, notes, and snippets.

@RyadPasha
Last active July 22, 2024 12:32
Show Gist options
  • Save RyadPasha/0fa629d0e4c9bdf8fa37e82dadf9b2eb to your computer and use it in GitHub Desktop.
Save RyadPasha/0fa629d0e4c9bdf8fa37e82dadf9b2eb to your computer and use it in GitHub Desktop.
Convert Arabic/Persian numbers to English numbers in JavaScript
function conv2EnNum(str){
return parseFloat(str
.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) { return d.charCodeAt(0) - 1632; }) // Convert Arabic numbers
.replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; }) // Convert Persian numbers
)*1;
}
// Examples:
alert(conv2EnNum('٠١٢٣٤٥٦٧٨٩')); // -> 01213456789
alert(conv2EnNum('١٣.٥')); // -> 13.5
alert(conv2EnNum('۴۷۶')); // -> 476
@aminrms
Copy link

aminrms commented May 12, 2024

What is the role of *1 at the end of the function?
😂

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