var a='1,125';
a=a.replace(/\,/g,''); // 1125, but a string, so convert it to number
a=parseInt(a,10);
Hope it helps.
Answer from web-nomad on Stack Overflowvar a='1,125';
a=a.replace(/\,/g,''); // 1125, but a string, so convert it to number
a=parseInt(a,10);
Hope it helps.
var a='1,125'
a=a.replace(/\,/g,'')
a=Number(a)
javascript - remove comma separation from the specific number from JS - Stack Overflow
Removing commas in a string
javascript - How do you remove the comma from a number? - Stack Overflow
Remove commas from the string using JavaScript - Stack Overflow
Hi all, I'm trying to remove commas from my numbers that are in an array :
I know the method '.toFixed(2)' but I'm having trouble applying it in a .map
This is what I did:
And this is what I get :
Unfortunately I added the two values so I don't get an array anymore.
What would be the right way to find an array like I had but without a comma?
How do I merge them into my array? 🙂
You can simplify the regular expression:
num.replace(/,/g, '')
Replace the regex in the replace method with /,/g which means matches the character,literally (case sensitive)
var num = '12,312,313,214,214,324.89';
var num2 = '12,312,313,214,214,324';
function replaceComma(num) {
return num.replace(/,/g, '');
};
console.log(replaceComma(num));
console.log(replaceComma(num2));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can try using replace. The g in the function will find all instances of "," in the string and replace it with nothing.
var mystring = "1,000,000";
mystring = mystring.replace(/,/g,"")
console.log(mystring);
Can't you do something like this then,
Altough the dollar sign is not included:
var num="1,000,00";
var getin=true;
var concatenate="";
for (var i=0; i<num.length; i++)
{
if (num[i]=="," && getin){ getin=false;continue;}
concatenate+=num[i];
}
alert(parseInt(concatenate));
It would also work for values greater then 9999 ect..
To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:
var total = parseFloat('100,000.00'.replace(/,/g, '')) +
parseFloat('500,000.00'.replace(/,/g, ''));
Related answer, but if you want to run clean up a user inputting values into a form, here's what you can do:
const numFormatter = new Intl.NumberFormat('en-US', {
style: "decimal",
maximumFractionDigits: 2
})
// Good Inputs
parseFloat(numFormatter.format('1234').replace(/,/g,"")) // 1234
parseFloat(numFormatter.format('123').replace(/,/g,"")) // 123
// 3rd decimal place rounds to nearest
parseFloat(numFormatter.format('1234.233').replace(/,/g,"")); // 1234.23
parseFloat(numFormatter.format('1234.239').replace(/,/g,"")); // 1234.24
// Bad Inputs
parseFloat(numFormatter.format('1234.233a').replace(/,/g,"")); // NaN
parseFloat(numFormatter.format('$1234.23').replace(/,/g,"")); // NaN
// Edge Cases
parseFloat(numFormatter.format(true).replace(/,/g,"")) // 1
parseFloat(numFormatter.format(false).replace(/,/g,"")) // 0
parseFloat(numFormatter.format(NaN).replace(/,/g,"")) // NaN
Use the international date local via format. This cleans up any bad inputs, if there is one it returns a string of NaN you can check for. There's no way currently of removing commas as part of the locale (as of 10/12/19), so you can use a regex command to remove commas using replace.
ParseFloat converts the this type definition from string to number
If you use React, this is what your calculate function could look like:
updateCalculationInput = (e) => {
let value;
value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
if(value === 'NaN') return; // locale returns string of NaN if fail
value = value.replace(/,/g, ""); // remove commas
value = parseFloat(value); // now parse to float should always be clean input
// Do the actual math and setState calls here
}