Created
April 15, 2015 01:56
-
-
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
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
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