-
-
Save allex/2e86b35bd3cc3ddfd58810b88392d7ed to your computer and use it in GitHub Desktop.
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 reverseFormatNumber(val,locale){ | |
var parts = new Intl.NumberFormat(locale).formatToParts(1111.1); | |
var group = parts.find(part => part.type === 'group').value; | |
var decimal = parts.find(part => part.type === 'decimal').value; | |
var reversedVal = val.replace(new RegExp('\\' + group, 'g'), ''); | |
reversedVal = reversedVal.replace(new RegExp('\\' + decimal, 'g'), '.'); | |
return Number.isNaN(reversedVal)?0:+reversedVal; | |
} | |
console.log(reverseFormatNumber('1,234.56','en')); | |
console.log(reverseFormatNumber('1.234,56','de')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment