Does String.Format ignore NumberFormatInfo?

I am outputting data from a process in csv. I store intermediate results in a data class, which also has methods for outputting data to a string so that it can be written to a file.

Class DataClass { // the actual data public double Value1 { get; set; } public double Value2 { get; set; } public double Value3 { get; set; } // return headers for this dataclass (called once) public static string Headers { get { return "Value1\tValue2\tValue3"; } } // no decimals needed (keep filesize smaller, numbers are millions and up) static NumberFormatInfo nfi = new NumberFormatInfo() { NumberDecimalDigits = 0 }; // this returns a string with the values for the dataclass (called for each row) public string ValuesString { get { // ** I would expect the numbers to have NO decimals because of nfi ** return string.Format(nfi, "{0}\t{1}\t{2}", Value1, Value2, Value3, ); } } } 

Here I write to the file:

 // headers swResultFile.WriteLine("Group\t" + GroupResult.Headers); // values foreach (var r in GroupResults) swResultFile.WriteLine(r.Key + "\t" + r.Value.ValuesString); 

But the output file still has decimal places:

 Group Value1 Value2 Value3 1 176983.222718191 278477.364780645 462811.208871335 2 11262339.27 16383.9680721473 118430.334721429 

Does anyone know why it is ignoring NumberFormatInfo?

When I test it in the debugger, it looks just fine.

Thanks,

Geert Yang

+6
source share
1 answer

To use NumberFormatInfo , you need to use the N format specifier for Format . try it

  return string.Format(nfi, "{0:N}\t{1:N}\t{2:N}", Value1, Value2, Value3, ); 
+11
source

Source: https://habr.com/ru/post/890046/


All Articles