0

I have a csv file with the split character as ; and the decimal Separator as ,. I use the csv file in my Unit Tests (NUnit Framework) for assertions. The issue is that my development machine has , as decimal separator and Server has . as decimal separator. For example 1,314 becomes 1314 in my server and the assertions fail in the test. Is there a solid solution for this? Below is a snippet showing how I read from .csv file, and store in a DataTable table

using (StreamReader reader = new StreamReader(filePath))
{ 
    while (!reader.EndOfStream)
    {
        string[] rows = reader.ReadLine().Split(';');
        DataRow dataRow = table.NewRow();
        for (int i = 0; i < headers.Length; i++)
        {
            dataRow[i] = rows[i];
        }
        table.Rows.Add(dataRow);
    }           
 }        

And an example of assertion, where expectedResultTable is a result of Reading the csv file into a DataTable.

 Assert.AreEqual(Math.Round(Convert.ToDouble(expectedResultTable.Rows[1]["DATA2"]), 2), Math.Round(manuversResult[1].Amplitude,2));
Abhishek Kumar
  • 342
  • 5
  • 22
  • Might be worth setting the culture of the current thread at the start of the test(s) – Vitani Mar 06 '17 at 11:22
  • Decimal separator is tied to the culture. If your code is to only process data from a specific country, maybe your code should always convert the input data to the expected culture before processing it. However, if your code suppose to handle multiple cultures you might need to pass the culture along with the file to the code processing it. – Nope Mar 06 '17 at 11:25
  • @TimSchmelter the data is stored as a string and converted as double – Abhishek Kumar Mar 06 '17 at 11:26
  • @AbhishekKumar Have a look at this post which should show you how to apply a specific culture when converting a string to a double, which hopefully helps ► [**How to convert string to double with proper cultureinfo**](http://stackoverflow.com/questions/2583362/how-to-convert-string-to-double-with-proper-cultureinfo) – Nope Mar 06 '17 at 11:28
  • There is no solution to that. Consider using another format than CSV. CSV is not a standard. – Bart Friederichs Mar 06 '17 at 11:29
  • The easiest solution is to **stick to neutral culture** whenever you have to save something used by your application: csv files, databases, configurations, etc. If you must display something to the user in his preferable (configurable?) language, then it's another button to generate report for the user. Otherwise user either shouldn't access this internal csv used to client-server interaction or to be aware it's neutral culture. Another option is to pass culture somehow (e.g. as a part of file name). – Sinatr Mar 06 '17 at 12:24

0 Answers0