Javascript: convert string representation of money to number

Suppose I have a quantity in a string format:

amount = '12,000.00' 

I want to convert it to Number (Javascript) or float.

 parseFloat(amount) // this gives me 12 as a result Number(amount) // this gives me NaN as a result 

Another solution I thought:

 parseFloat(amount.replace(/[,]/g, '')) 

It works great. But the problem is here at Locale.

This will not work if the amount is € 12,000.00. Here ',' has a completely different meaning.

I was looking for a good solution, but could not. I am looking for a generic solution.

+4
source share
3 answers

You can get the local decimal separator as follows:

 1.1.toLocaleString().substr(1,1) 

Before processing parse float, you can make sure that the string contains only digits, possibly a minus sign, and a local decimal separator.

0
source

In truth, you'll never know the format. 12,345 . Is it 12345 or another locale version if 12.345 ?

However, if you have consecutive decimal numbers, you can use the lastIndexOf function in a comma, and the period will show the decimal position and character.

 var price = '12,345.67'; var lastPeriod = price.lastIndexOf('.'); var lastComma = price.lastIndexOf(','); if (lastComma != -1 && lastComma > lastPeriod) { decimalCharacter = ','; } else { decimalCharacter = '.'; } console.log(decimalCharacter); //. or , based on how the price string looks - see below 

If price is 12,345.67 , decimalCharacter will be . . If he is 12.345,67 , he will be returned as 12.345,67

0
source

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

0
source

Source: https://habr.com/ru/post/1482909/


All Articles