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 - How to add comma in a number in string - Stack Overflow
Javascript Formatting numbers with commas
String to number - comma separator
javascript - How can I format a number with commas as thousands separators? - Stack Overflow
Videos
You can specify the exact number of decimal digits in your options, which is the second parameter in toLocaleString()
const number = 24242324.5754;
number.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})
// result is: 24,242,324.58
See also MDN doc here
minimumFractionDigits
The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is
maximumFractionDigits
The maximum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number formatting is the larger of minimumFractionDigits and 3
The toFixed() method returns a string but the toLocaleString() method expects a number so just convert your string to a number after using the toFixed() method with the parseFloat() function and then use the toLocaleString() method on it.
However, do note that you will have to manually append the leading 0 since the parseFloat() method removes any leading zeroes to the right of the decimal point.
Check this particular answer on another Stack Overflow thread that explains the reason why the parseFloat() method removes the leading zeroes after the decimal point.
var num = 66666.7
var parsedNum = (""+num).split('.')[1].length > 1 ?
parseFloat(num.toFixed(2)).toLocaleString() :
parseFloat(num.toFixed(2)).toLocaleString() + '0';
console.log("original", num)
console.log("with comma", num.toLocaleString())
console.log("with 2 digit fixed", num.toFixed(2))
console.log("now working--", parsedNum)
I think this function will take care of all the issues related to this problem.
function commaFormat(inputString) {
inputString = inputString.toString();
var decimalPart = "";
if (inputString.indexOf('.') != -1) {
//alert("decimal number");
inputString = inputString.split(".");
decimalPart = "." + inputString[1];
inputString = inputString[0];
//alert(inputString);
//alert(decimalPart);
}
var outputString = "";
var count = 0;
for (var i = inputString.length - 1; i >= 0 && inputString.charAt(i) != '-'; i--) {
//alert("inside for" + inputString.charAt(i) + "and count=" + count + " and outputString=" + outputString);
if (count == 3) {
outputString += ",";
count = 0;
}
outputString += inputString.charAt(i);
count++;
}
if (inputString.charAt(0) == '-') {
outputString += "-";
}
//alert(outputString);
//alert(outputString.split("").reverse().join(""));
return outputString.split("").reverse().join("") + decimalPart;
}
If you're looking for a short and sweet solution:
const number = 12345678.99;
const numberString = String(number).replace(
/^\d+/,
number => [...number].map(
(digit, index, digits) => (
!index || (digits.length - index) % 3 ? '' : ','
) + digit
).join('')
);
// numberString: 12,345,678.99
I'm having javascript do number calculations for me. I need to insert a comma between ever 3rd number. For instance, the number currently appears as $1234567.89 but I need it to appear as $1,234,567.89. How do I fix it? This my current code:
"$ " + (var1 + var2).toFixed(2)
Thanks!
*grammar