This is not so simple, because you cannot know exactly what the separator is for thousands and what for the decimal part
Consider "12.000.000" is it 12000.000 === 12000 or 12000000 ?
But if you set the requirement that the last separator always be a decimal separator - the value if at least one separator is specified, the last one has a decimal separator * if the following digits do not exceed a certain length.
Then you can try the following
Edit
(see revs if you are interested in the old function)
I set the ability to determine the maximum length of digits after the last separator "," or ".". until it is processed as a float, after which it will be returned as an integer
var amounts = ["12000","12.000,00", "12,000.00", "12,000,01", "12.000.02", "12,000,001"]; formatMoney.maxDecLength = 3; //Set to Infinity os to disable it function formatMoney(a) { var nums = a.split(/[,\.]/); var ret = [nums.slice(0, nums.length - 1).join("")]; if (nums.length < 2) return +nums[0]; ret.push(nums[nums.length - 1]); return +(ret.join(nums[nums.length - 1].length < formatMoney.maxDecLength ? "." : "")); } for ( var i=0,j;j=amounts[i];i++) console.log (j + " -> " +formatMoney(j));
Gives output:
"12000 -> 12000" "12.000,00 -> 12000" "12,000.00 -> 12000" "12,000,01 -> 12000.01" "12.000.02 -> 12000.02" "12,000,001 -> 12000001" //as you can see after the last "," there are 3 digits and its treated as integer
Another JSBin