c# - 什么是 ToString ("N0") 格式?

标签 c# .net number-formatting

此代码来自 Charles Pettzold 的“Programming Windows Sixth Edition”一书:

public object Convert(object value, Type targetType, object parameter, string language) 
{ 
    return ((double)value).ToString("N0"); 
}

ToString("N0") 应该打印带有逗号分隔符且没有小数点的值。我在文档中找不到对适当的 ToString 重载和 "N0" 格式的引用。请将我指向 .NET 文档中的正确位置。

最佳答案

检查 following article在 MSDN 上关于 N 格式的例子。这也包含在 Standard Numeric Format Strings 中。文章。

相关摘录:

//       Formatting of 1054.32179:
//          N:                     1,054.32 
//          N0:                    1,054 
//          N1:                    1,054.3 
//          N2:                    1,054.32 
//          N3:                    1,054.322 

When precision specifier controls the number of fractional digits in the result string, the result string reflects a number that is rounded to a representable result nearest to the infinitely precise result. If there are two equally near representable results:

  • On the .NET Framework and .NET Core up to .NET Core 2.0, the runtime selects the result with the greater least significant digit (that is, using MidpointRounding.AwayFromZero).
  • On .NET Core 2.1 and later, the runtime selects the result with an even least significant digit (that is, using MidpointRounding.ToEven).

关于c# - 什么是 ToString ("N0") 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16209509/

相关文章:

c# - 如何根据值选择ItemTemplate?

c# - 我可以从 SQL Reporting Services (.rdlc) 生成基于图像的 PDF 报告而不将图像保存在任何地方吗?

c# - 如何使用 FluentNHibernate 映射具有复杂键类型 (CultureInfo) 的字典

c# - C# 8 是否支持 .NET Framework?

c# - 如何放置千位分隔符 "point"

javascript - Knockout.js 在 ko.observable() 写入之前修改值

c# - 每个 HTTP 请求一个类的单个实例

c# - 创建数据列而不是行

.net - 在运行时运行 MSBuild

java - 如何使用 DecimalFormat 方法更改小数点前数字的大小