Convert String with Comma to Integer

How can I parse a string with a comma thousand separator to a number?

Yes remove the commas:

let output = parseFloat("2,299.00".replace(/,/g, ''));
console.log(output);

Python parse comma-separated number into int

>>> a = '1,000,000'
>>> int(a.replace(',', ''))
1000000
>>>

How to convert a string of comma separated numbers to integers?

First split the values with comma using the split() method like this.

arr.split(',')

After that you will get each value separated in an array which will be accessible by giving array index. So arr[0] will include 108 and arr[1] will include 109. Now all that's left to do is parse them individually.

parseInt(arr[0]) 
parseInt(arr[1])

Converting Integer to String with comma for thousands

System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646

Convert string with comma to integer

How about this?

 "1,112".delete(',').to_i

How to parse number string containing commas into an integer in java?

Is this comma a decimal separator or are these two numbers? In the first case you must provide Locale to NumberFormat class that uses comma as decimal separator:

NumberFormat.getNumberInstance(Locale.FRANCE).parse("265,858")

This results in 265.858. But using US locale you'll get 265858:

NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858")

That's because in France they treat comma as decimal separator while in US - as grouping (thousand) separator.

If these are two numbers - String.split() them and parse two separate strings independently.

Convert String (with comma) into integer

 '38,38'.split(',').join('.').to_f * 2
#=> 76.76

Another option is using String#sub (thanks @Stefan!):

'38,38'.sub(',', '.').to_f * 2
#=> 76.76


Related Topics



Leave a reply



Submit