MikeStall / DataTable

Class library for working with tabular data, especially CSV files
Apache License 2.0
150 stars 63 forks source link

[Help needed] CurrentCulture #57

Closed JakobMadsenICP closed 5 years ago

JakobMadsenICP commented 5 years ago

Saving a CSV on my system gives:

"12,9463774465272","0,270021829870012","0,729978170129988",19,"0,286047854156914","13,1529464379659","0,00113901030272245",239 323,"66,6",p11,6. november 2017 11:25:44
"11,3614570248825","0,707196280277479","0,292803719722521",14,"2,56015141460512","10,1980390271856","0,000166481564519927",216 318,"66,6",p11,6. november 2017 11:25:44
"9,83046913334565","0,495632250395642","0,504367749604358",5,"1,56187661833842","10,295630140987","0,122441418468952",178 288,"66,6",p11,6. november 2017 11:25:44

This is because I use a Danish cultureinfo. It uses "," as a decimal separator. I use the following code to save it in an en-US culture.

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
               DataTable dt = DataTable.New.FromEnumerable(measurements.Select(o => o.GetDataTransport()));
                dt.SaveCSV(saveFileName);

Giving:
10.6894273504936,0.271032847702691,0.728967152297309,16,0.361142918520389,11.1803398874989,0.353137403726578,202 170,66.6,Frivillige,"Monday, November 6, 2017 11:23:09 AM"
12.1445648012705,0.628536807366685,0.371463192633315,13,0.278776556972861,12.8062484748657,0.0546613968908787,248 159,66.6,Frivillige,"Monday, November 6, 2017 11:23:09 AM"

Is there a nicer way to handle this? Suggestion: dt.SaveCSV(saveFileName,new CultureInfo("en-US"))

MikeStall commented 5 years ago

You're welcome to submit a Pull Request. I think the issue is the ToString() call here that translates from primitives to strings: https://github.com/MikeStall/DataTable/blob/master/Sources/DataAccess/Utility.cs#L207

It doesn't take any culture info and so picks up the default. Perhaps just modifying this to always use en-US?