Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created April 15, 2015 01:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmpvar/0d483090f05136665d26 to your computer and use it in GitHub Desktop.
Save tmpvar/0d483090f05136665d26 to your computer and use it in GitHub Desktop.
turn a non-degenerate formatted monetary amount and convert it to a number
var input = [
'$10.000,00',
'$10,000.00',
'$10,000.00 ',
'$10,,,000.00 ',
'$10,0,0,0.00 ',
'$10,000',
'$10,000',
'$10000',
'10000',
10000,
];
input.map(function(s) {
return parseFloat(
String(s)
.replace(/\D/g, '-')
.split('-')
.filter(Boolean)
.map(function(part, i, a) {
if (i === a.length-1 && part.length === 2) {
return '.' + part;
}
return part;
}).join('')
);
}).map(function(r, i) {
console.log(String(input[i]), '=>', r);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment