Alex Rivera | Logout

Convert String with Dot or Comma as decimal separator to number in JavaScript

Asked 2011-09-15T13:42:08.947
74

An input element contains numbers a where comma or dot is used as decimal separator and space may be used to group thousands like this:

'1,2'
'110 000,23'
'100 1.23'

How would one convert them to a float number in the browser using JavaScript?

jQuery and jQuery UI are used. Number(string) returns NaN and parseFloat() stops on first space or comma.

Edit
Report

2 Answers

58

Do a replace first:

parseFloat(str.replace(',','.').replace(' ',''))
answered 2011-09-15T13:45:14.240
22

The perfect solution

accounting.js is a tiny JavaScript library for number, money and currency formatting.

Check this for ref

answered 2013-02-13T05:33:08.827

Your Answer