格式化数字字符串以添加逗号 - c#

ben*_*iii 2 c# string formatting

我知道这个问题之前已经多次回答,但我确信我的代码是正确的但是工作不正常.

string total = ds.Tables[0].Rows[0][0].ToString();
string test = string.Format("{0:N}", total);
lbl_totalValue.Text = test;
Run Code Online (Sandbox Code Playgroud)

此代码不会像我们希望的那样将逗号添加到我的值中.

谁能明白为什么?

Dmi*_*nko 6

当你放

  string total = ds.Tables[0].Rows[0][0].ToString();
Run Code Online (Sandbox Code Playgroud)

它表示隐式G("常规")格式字符串

  string total = ds.Tables[0].Rows[0][0].ToString("G");
Run Code Online (Sandbox Code Playgroud)

不要过早格式化:

  var total = ds.Tables[0].Rows[0][0];         // Value from table
  string test = string.Format("{0:N}", total); // Format total with "N" format string
Run Code Online (Sandbox Code Playgroud)