Yes remove the commas:
let output = parseFloat("2,299.00".replace(/,/g, ''));
console.log(output);
Answer from Sam on Stack OverflowYes remove the commas:
let output = parseFloat("2,299.00".replace(/,/g, ''));
console.log(output);
Removing commas is potentially dangerous because, as others have mentioned in the comments, many locales use a comma to mean something different (like a decimal place).
I don't know where you got your string from, but in some places in the world "2,299.00" = 2.299
The Intl object could have been a nice way to tackle this problem, but somehow they managed to ship the spec with only a Intl.NumberFormat.format() API and no parse counterpart :(
The only way to parse a string with cultural numeric characters in it to a machine recognisable number in any i18n sane way is to use a library that leverages CLDR data to cover off all possible ways of formatting number strings http://cldr.unicode.org/
The two best JS options I've come across for this so far:
- https://github.com/google/closure-library/tree/master/closure/goog/i18n
- https://github.com/globalizejs/globalize
javascript parseFloat '500,000' returns 500 when I need 500000 - Stack Overflow
parsefloat - Javascript parse float is ignoring the decimals after my comma - Stack Overflow
Parse Float and thousand separator
jquery - Convert String with Dot or Comma as decimal separator to number in JavaScript - Stack Overflow
parseFloat( theString.replace(/,/g,'') );
I don't know why no one has suggested this expression-
parseFloat( theString.replace(/[^\d\.]/g,'') );
Removes any non-numeric characters except for periods. You don't need custom functions/loops for this either, that's just overkill.
This is "By Design". The parseFloat function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the "75" portion.
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat
To fix this convert the commas to decimal points.
var fullcost = parseFloat($("#fullcost").text().replace(',', '.'));
javascript's parseFloat doesn't take a locale parameter. So you will have to replace , with .
parseFloat('0,04'.replace(/,/, '.')); // 0.04
How can I add a thousand separator to a parseFloat value?
For example - if the value is 96645 I would like to show as 96,645.
Thanks for any help
Do a replace first:
parseFloat(str.replace(',','.').replace(' ',''))
I realise I'm late to the party, but I wanted a solution for this that properly handled digit grouping as well as different decimal separators for currencies. As none of these fully covered my use case I wrote my own solution which may be useful to others:
function parsePotentiallyGroupedFloat(stringValue) {
stringValue = stringValue.trim();
var result = stringValue.replace(/[^0-9]/g, '');
if (/[,\.]\d{2}$/.test(stringValue)) {
result = result.replace(/(\d{2})$/, '.$1');
}
return parseFloat(result);
}
This should strip out any non-digits and then check whether there was a decimal point (or comma) followed by two digits and insert the decimal point if needed.
It's worth noting that I aimed this specifically for currency and as such it assumes either no decimal places or exactly two. It's pretty hard to be sure about whether the first potential decimal point encountered is a decimal point or a digit grouping character (e.g., 1.542 could be 1542) unless you know the specifics of the current locale, but it should be easy enough to tailor this to your specific use case by changing \d{2}$ to something that will appropriately match what you expect to be after the decimal point.
Try:
parseFloat('1,022.55'.replace(/,/g, ''))
Here it is annotated
originalNum = '1,022.55';
cleanNum = originalNum.replace(",", "");
float = parseFloat(cleanNum);
console.log(float);
Alternatively you can just make it a one-liner by using
float = parseFloat('1,022.55'.replace(",", ""));
console.log(float);
You need to replace (remove) the dots first in the thousands separator, then take care of the decimal:
function isNumber(n) {
'use strict';
n = n.replace(/\./g, '').replace(',', '.');
return !isNaN(parseFloat(n)) && isFinite(n);
}
var number = parseFloat(obj.value.replace(",",""));
You'll probably also want this to go the other way...
obj.value = number.toLocaleString('en-US', {minimumFractionDigits: 2});