2

Problem:

I got this input:

"23,234.34"

with the following locale, locale = "us-US"

I want this result : 23234.34 in number var.

So i use parseFloat but this function don't take a locale as parameter, When i use the string "23,234.34", the function return Not a number (NAN).

if ($('input#feesamount').val() != '') {
            $funding = {
              feeAmount: parseFloat($('input#feesamount').val()),
              acquirerregistrationid: $('input#registerid').val(),
              acquirer: $('select#acquirer').val()
            };

Do you know a function who can help me ?

  • http://stackoverflow.com/a/12694511/ – chridam Oct 17 '14 at 09:53
  • possible duplicate of [JavaScript parseFloat in Different Cultures](http://stackoverflow.com/questions/12694455/javascript-parsefloat-in-different-cultures) – bitoiu Oct 17 '14 at 09:56

3 Answers3

1

If you are only dealing with US formats, you can just use string.replace(/,/g, ''). Otherwise, you want to use this library : http://numeraljs.com/

Thomas Ruiz
  • 3,611
  • 2
  • 20
  • 33
0

You could write your own that uses a switch to process the price/fee depending on locale and returns a float.

var price = '23,234.34';
var locale = 'us-US';

function parsePrice(price, locale) {
    switch (locale) {
        case 'us-US':
            price = price.replace(',', '');
            break;
    }
    return parseFloat(price);
}

parsePrice(price, locale); // 23234.34

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0

Try to replace the ',' with ''(Empty String)

if ($('input#feesamount').val() != '') {
        $funding = {
          feeAmount: parseFloat(($('input#feesamount').val()).replace(/,/g,'')),
          acquirerregistrationid: $('input#registerid').val(),
          acquirer: $('select#acquirer').val()
        };
Swaraj Ghosh
  • 2,284
  • 3
  • 16
  • 19