views:

586

answers:

4

Hello Everybody:

I'm importing a csv file in C#, sometimes with '.', sometimes with ',' as decimal separator.

Is there a best way of determinate the decimal separator better than counting from the last char down to the first apperance?

Thanks in advance.

Franklin Albricias.

+1  A: 

Why not use both as a separator?

Have a look at NumberFormatInfo

Edit:
For each value try to parse it with one of the separators.
If that fails try to parse it with the other.

Sani Huttunen
Then how do you tell which commas separate values, and which commas separate fractioal parts of numbers?
pavium
It's not my decision, it's the user one. Some users will export from Excel with ',' some will do it with '.'.That's why I need to detect the decimal separator.
Franklin Albricias
You probably can't. Not 100% anyway. Unless the values are enclosed in quotes or something else.
Sani Huttunen
Or the value separator is not comma.
Sani Huttunen
Ok thank you, NumberFormatInfo.GetDecimalSeparator will do it!Thank you again.
Franklin Albricias
A: 

This depends on the actual data stored in the csv file and the data separation character (';' or ',' or ' ').

If all data is always in floting point notation you can use a regular expression that checks both cases. You can use "d+,\d+" to check for values separated by ',' or "\d+\.\d+" for values using '.' as separator

Frank Bollack
+5  A: 

If you know the correct culture in advance (for example, because you know the user that created the file), you can try to parse the provided value using the appropriate CultureInfo or NumberFormatInfo:

Decimal value = Decimal.Parse(input, new CultureInfo("es-ES"));

But if the type is not known in advance, you'll have to check it manually by examining the characters until you find a separator. (And even that approach assumes that you are guaranteed to always have a decimal separator, such that one is written as 1.0 rather than 1.)

You can't just try each expected format one after the other because you may get false positives.

10,000 means something valid but different for both formats.

Jeff Sternal
A: 

Under the assumption that the file contains only numbers - no strings and what ever - and there are at least two columns, you can do the following.

Go through the first line and look for a semicolon. If you find one, you have semicolon separated numbers with commas as decimal separator, else comma separated numbers with points as decimal separator.

In all other cases you will have to use a heuristic (and sometimes get the wrong conclusion) or you have to strictly parse the file under both assumptions.

Daniel Brückner