gpt4 book ai didi

c# - 为什么 File.WriteAllText() 通过添加逗号来格式化我的数字?

转载 作者:行者123 更新时间:2023-11-30 14:58:00 26 4
gpt4 key购买 nike

我不明白为什么会这样,但确实如此。我正在写一个这样的 csv 文件:

foreach (var row in record.rows)
{
csv += "\n";
csv += row.FooName + ",";
csv += row.FooMoney + ",";
}

System.IO.File.WriteAllText(filePath + fileName, csv);

这就是有趣的地方。调试时,row.FooMoney(十进制类型)可能是 10000。在书面 csv 中它是 10,000,自然,我不想要逗号作为分隔符。

我试过 row.FooMoney.ToString("F"/"G") 无济于事...它似乎在文件写入期间被格式化。是什么赋予了?这是故意的吗?

更新


这是与问题相关的所有代码:

public ActionResult PassJSON(string __obj)
{
var filePath = Url.Content("~/Temp/");
var fileName = "Report.csv";

try
{
string csv = string.Empty;
var records = JsonConvert.DeserializeObject<List<RootReportObject>>(__obj);
csv += "Date Created,Ref #,Name,Vehicle #,Address,ProductID,Product,Weight1,Weight2,";

foreach (var record in records)
{
var count = record.count;
var value = record.title;

csv += "\n" + value + ",,,,,,,,,";

foreach (var row in record.rows)
{
csv += "\n";
csv += row.DateCreated + ",";
csv += row.RefNo + ",";
csv += row.Name + ",";
csv += row.VehicleNo + ",";
csv += row.Address1 + ",";
csv += row.ProductID + ",";
csv += row.Product + ",";
csv += row.Weight1.ToString(CultureInfo.InvariantCulture) + ",";
csv += row.Weight2.ToString(CultureInfo.InvariantCulture) + ",";
}
}

System.IO.File.WriteAllText(filePath + fileName, csv);

return Json(filePath + fileName, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json(e, JsonRequestBehavior.AllowGet);
}
}

[Serializable]
public class Row
{
public int id { get; set; }
public DateTime DateCreated { get; set; }
public int RefNo { get; set; }
public string Name { get; set; }
public string VehicleNo { get; set; }
public string Address1 { get; set; }
public int? ProductID { get; set; }
public string Product { get; set; }
public Decimal Weight1 { get; set; }
public Decimal Weight2 { get; set; }
}

[Serializable]
public class RootReportObject
{
public bool __group { get; set; }
public int level { get; set; }
public int count { get; set; }
public string value { get; set; }
public string title { get; set; }
public int collapsed { get; set; }
public List<Row> rows { get; set; }
public object groups { get; set; }
public string groupingKey { get; set; }
}

示例输出

8/16/2013 2:31:16 PM,72,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/16/2013 2:31:34 PM,73,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,5,000,0,
8/16/2013 2:32:12 PM,74,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/16/2013 2:33:15 PM,75,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/16/2013 2:50:58 PM,76,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/20/2013 7:19:32 PM,77,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/20/2013 7:46:03 PM,78,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,40,
8/20/2013 7:55:56 PM,79,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,1,
8/20/2013 8:13:58 PM,80,John Doe,,9021 Beverly Hills Blvd,427,Corn,0,50,
8/21/2013 8:05:25 PM,81,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,5,
8/22/2013 7:33:03 PM,82,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/22/2013 7:40:42 PM,83,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,5,
8/22/2013 8:05:25 PM,84,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,5,000,

更新 #2


我删除了原始的 report.csv,发现下一次编写报告的调用从 IIS 中产生了 404 错误。显然 WriteAllText() 不再写入。不幸的是,我也不确定该怎么做。我已经更改了代码,试图在写入发生之前删除任何以前的版本,但仍然一无所获

修改后的代码

if (System.IO.File.Exists(filePath + fileName))
System.IO.File.Delete(filePath + fileName);

System.IO.File.WriteAllText(filePath + fileName, csv);
return Json(filePath + fileName, JsonRequestBehavior.AllowGet);

最佳答案

使用 row.FooMoney.ToString("f") 应该可以防止千位分隔符被写入。

如果您使用覆盖默认行为的奇怪文化设置,您可以通过 row.FooMoney.ToString("f", CultureInfo.InvariantCulture) 强制执行。

请注意,如果您使用的是 .NET 4 或更高版本,您可以通过以下方式提高效率:

System.IO.File.WriteAllLines(System.IO.Path.Combine(filePath, fileName),
record.Rows.Select(row => row.FooName + "," + row.FooMoney.ToString("f")));

关于c# - 为什么 File.WriteAllText() 通过添加逗号来格式化我的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com