0

Writing a function to read a wide array of CSV datasets. Can parse CSV to arrays-of-arrays-of-strings well enough (see Javascript code to parse CSV data).

Now would like to can each column, and if they're all convertible to numbers, convert them to Number, else leave as String. But if not, leave them as strings.

Of course, numbers can come in many formats, some possibly dependent on locale. I'd like a function like parseFloat that recognizes these:

"2.0"      -> 2.0
"2"        -> 2.0
"1,532"    -> 1532 (in US at least)
"$2.00"    -> 2.0
"0.02"     -> 0.02
"2%"       -> 0.02
""         - null
"NaN"      -> NaN

"""NaN"""      -> String
"2 years old"  -> String
"ABC123"  ->  String

parseFloat(val) doesn't quite work for this purpose:

parseFloat("1,523") === 1
parseFloat("2%") === 2        // could live with this
parseFloat("$2.25") === NaN

This obviously too much to expect a native ECMAscript function to handle, but it seems like i shouldn't be inventing regular expressions myself for each of these either.

Might there be a library that does this well? My Google search skills seem to fail here.

Handling common date formats would be even better.

Community
  • 1
  • 1
prototype
  • 7,249
  • 15
  • 60
  • 94

2 Answers2

1

As you and others have pointed out, this problem can be solved with regular expressions. But if you want a library to do it for you, the jQuery Number Formatter library should allow you to do what you want. It handles numbers (including percentages) but not dates: https://code.google.com/p/jquery-numberformatter/

It requires both jQuery and jshashtable.

ASGM
  • 11,051
  • 1
  • 32
  • 53
1

Just use a simple regex and wrap it up, pretty simple

For example:

function parseToNumeric(value) {
    return parseFloat(value.replace(/[\$,%]/g, ''));
}
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • This is elegant. I need not parse the different formats, just strip out the difficulties. There are locale specific formats that the library handles but can live without those. So, will accept the other answer which mentions a reputable library as asked, but actually use this approach. Thanks!! – prototype Mar 14 '13 at 19:41