的String.Format忽略的NumberFormatInfo? [英] string.Format ignores NumberFormatInfo?
本文介绍了的String.Format忽略的NumberFormatInfo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的输出数据。余存储中间结果的数据的类,其中也有方法来输出的数据为字符串,因此它可以被写入到文件中。
I output data from a process to a csv. 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 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:
Group Value1 Value2 Value3
1 176983.222718191 278477.364780645 462811.208871335
2 11262339.27 16383.9680721473 118430.334721429
有谁知道为什么它忽略的NumberFormatInfo?
Does anyone know why it's ignoring the NumberFormatInfo?
当我在调试器中检查它,它看起来就好了。
When I check it in the debugger it looks just fine.
谢谢
格特 - 扬
推荐答案
您必须使用 N
格式说明符格式
使用的NumberFormatInfo
。试试这个
You have to use the N
format specifier for Format
to use the NumberFormatInfo
. Try this
return string.Format(nfi, "{0:N}\t{1:N}\t{2:N}",
Value1,
Value2,
Value3,
);
这篇关于的String.Format忽略的NumberFormatInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文