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
Ruby Way to Group Anagrams in String Array
How to Validate the Presence of a Belongs to Association with Rails
Differencebetween #Encode and #Force_Encoding in Ruby
Escape Double and Single Backslashes in a String in Ruby
How to Add Confirm Message with Link_To Ruby on Rails
How to Turn a Ruby Method into a Block
Rvm Install: Ruby Installation Error
Converting Nested Hash Keys from Camelcase to Snake_Case in Ruby
Ruby: How to Find Non-Unique Elements in Array and Print Each with Number of Occurrences
Get Link and Href Text from HTML Doc with Nokogiri & Ruby
How to Replace the Last Occurrence of a Substring in Ruby
How to Find Where a Ruby Method Is Declared
Set the Display Precision of a Float in Ruby
Nokogiri To_Xml Without Carriage Returns