Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does File.WriteAllText() format my numbers by adding commas?

Tags:

c#

file-io

csv

I can't see why this would be happening but it is. I'm writing a csv file like this:

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

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

Here's what's fun. When debugging, row.FooMoney (which is of type Decimal) might be 10000. In the written csv it is 10,000 which, naturally, I do not want as commas are my delimiter.

I've tried row.FooMoney.ToString("F"/"G") to no avail...it appears to be formatted during the file write. What gives? Is this intentional?

UPDATE


Here's all the code associated with the issue:

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; }
}

Sample Output

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,

UPDATE #2


I deleted the original report.csv to find that the next call to write the report yielded a 404 error from IIS. Evidently the WriteAllText() is no longer writing. Unfortunately I'm not sure what to do about this either. I've altered the code in an attempt to remove any previous version before the write occurs but still get nothing

Altered Code

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);
like image 316
Mike H. Avatar asked Nov 19 '25 05:11

Mike H.


1 Answers

Using row.FooMoney.ToString("f") should prevent the thousands separators from being written.

If you're using an odd culture setting which is overriding the default behavior, you can force this via row.FooMoney.ToString("f", CultureInfo.InvariantCulture).

Note that, if you're using .NET 4 or later, you can make this far more efficient via:

System.IO.File.WriteAllLines(System.IO.Path.Combine(filePath, fileName),
   record.Rows.Select(row => row.FooName + "," + row.FooMoney.ToString("f")));
like image 162
Reed Copsey Avatar answered Nov 21 '25 19:11

Reed Copsey