Last active
July 22, 2024 12:32
-
-
Save RyadPasha/0fa629d0e4c9bdf8fa37e82dadf9b2eb to your computer and use it in GitHub Desktop.
Convert Arabic/Persian numbers to English numbers in JavaScript
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
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
commented
May 12, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment