Skip to content

Instantly share code, notes, and snippets.

@vbresan
Created October 26, 2021 14:55
Show Gist options
  • Save vbresan/11b9b12a510a8996d58e92347e683acc to your computer and use it in GitHub Desktop.
Save vbresan/11b9b12a510a8996d58e92347e683acc to your computer and use it in GitHub Desktop.
Simplifies string in currency format. Removes all thousands separators, keeps only decimal separator. Separators can be "," or ".".
function parseInputNumber(value) {
let last = Math.max(value.lastIndexOf(","), value.lastIndexOf("."));
if (last == -1) {
return value;
}
let parts = value.split(/[.,]/);
let parsed = parts.slice(0, -1).join("");
if (parts.slice(-1)[0].length <= 2) {
parsed += value[last];
}
parsed += parts.slice(-1);
return parsed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment