[英]string.Format ignores NumberFormatInfo?
I output data from a process to a csv.我从一个进程到 csv 的 output 数据。 I store intermediate results in a data class, which also has methods to output the data to a string so it can be written to a file.
我将中间结果存储在数据 class 中,该数据还具有将 output 数据写入字符串的方法,因此可以将其写入文件。
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 all the decimals:但是 output 文件仍然包含所有小数:
Group Value1 Value2 Value3
1 176983.222718191 278477.364780645 462811.208871335
2 11262339.27 16383.9680721473 118430.334721429
Does anyone know why it's ignoring the NumberFormatInfo?有谁知道它为什么忽略 NumberFormatInfo ?
When I check it in the debugger it looks just fine.当我在调试器中检查它时,它看起来很好。
Thanks,谢谢,
Gert-Jan格特-扬
You have to use the N
format specifier for Format
to use the NumberFormatInfo
.您必须对
Format
使用N
格式说明符才能使用NumberFormatInfo
。 Try this尝试这个
return string.Format(nfi, "{0:N}\t{1:N}\t{2:N}",
Value1,
Value2,
Value3,
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.