c# – string.Format忽略NumberFormatInfo?
发布时间:2020-12-15 03:53:17 所属栏目:百科 来源:网络整理
导读:我将数据从进程输出到csv.我将中间结果存储在数据类中,该数据类还具有将数据输出到字符串的方法,以便将其写入文件. Class DataClass { // the actual data public double Value1 { get; set; } public double Value2 { get; set; } public double Value3 { g
我将数据从进程输出到csv.我将中间结果存储在数据类中,该数据类还具有将数据输出到字符串的方法,以便将其写入文件.
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 "Value1tValue2tValue3"; } } // 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,); } } } 在这里我写信给文件: // headers swResultFile.WriteLine("Groupt" + GroupResult.Headers); // values foreach (var r in GroupResults) swResultFile.WriteLine(r.Key + "t" + r.Value.ValuesString); 但输出文件仍然包含所有小数: Group Value1 Value2 Value3 1 176983.222718191 278477.364780645 462811.208871335 2 11262339.27 16383.9680721473 118430.334721429 有谁知道它为什么忽略NumberFormatInfo? 当我在调试器中检查它时它看起来很好. 谢谢, 格特 – 扬 解决方法
您必须使用Format的N格式说明符才能使用NumberFormatInfo.试试这个
return string.Format(nfi,"{0:N}t{1:N}t{2:N}",); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读