Skip to content

Instantly share code, notes, and snippets.

@allex
Forked from OliverJAsh/foo.js
Created May 25, 2021 14:56
Show Gist options
  • Save allex/2e86b35bd3cc3ddfd58810b88392d7ed to your computer and use it in GitHub Desktop.
Save allex/2e86b35bd3cc3ddfd58810b88392d7ed to your computer and use it in GitHub Desktop.
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