簡體   English   中英

string.Format 忽略 NumberFormatInfo?

[英]string.Format ignores NumberFormatInfo?

我從一個進程到 csv 的 output 數據。 我將中間結果存儲在數據 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,
            );
        }
    }
}

在這里我寫入文件:

// headers
swResultFile.WriteLine("Group\t" + GroupResult.Headers);

// values
foreach (var r in GroupResults) swResultFile.WriteLine(r.Key + "\t" + r.Value.ValuesString);

但是 output 文件仍然包含所有小數:

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}",
            Value1,
            Value2,
            Value3,
        );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM