设置数值结果表的格式(C# 参考)Formatting numeric results table (C# Reference)

下表显示了设置数值结果格式的受支持格式说明符。The following table shows supported format specifiers for formatting numeric results. 最后一列中的格式化结果对应于“en-US”CultureInfoThe formatted result in the last column corresponds to the "en-US" CultureInfo.

格式说明符Format specifier 说明Description 示例Examples 结果Result
C 或 cC or c 货币Currency string s = $"{2.5:C}";

string s = $"{-2.5:C}";
$2.50$2.50

($2.50)($2.50)
D 或 dD or d 十进制Decimal string s = $"{25:D5}"; 0002500025
E 或 eE or e 指数Exponential string s = $"{250000:E2}"; 2.50E+0052.50E+005
F 或 fF or f 定点Fixed-point string s = $"{2.5:F2}";

string s = $"{2.5:F0}";
2.502.50

33
G 或 gG or g 常规General string s = $"{2.5:G}"; 2.52.5
N 或 nN or n NumericNumeric string s = $"{2500000:N}"; 2,500,000.002,500,000.00
P 或 pP or p 百分比Percent string s = $"{0.25:P}"; 25.00%25.00%
R 或 rR or r 往返过程Round-trip string s = $"{2.5:R}"; 2.52.5
X 或 xX or x 十六进制Hexadecimal string s = $"{250:X}";

string s = $"{0xffff:X}";
FAFA

FFFFFFFF

备注Remarks

使用格式说明符可以创建格式字符串。You use a format specifier to create a format string. 格式字符串的格式如下:Axx,其中The format string is of the following form: Axx, where

  • A 是格式说明符,控制应用于数值的格式设置类型。A is the format specifier, which controls the type of formatting applied to the numeric value.
  • xx 是精度说明符,影响格式化输出中的位数。xx is the precision specifier, which affects the number of digits in the formatted output. 精度说明符值的范围为 0 到 99。The value of the precision specifier ranges from 0 to 99.

十进制(“D”或“d”)和十六进制(“X”或“x”)格式说明符仅支持用于整型类型。The decimal ("D" or "d") and hexadecimal ("X" or "x") format specifiers are supported only for integral types. 往返(“R”或“r”)格式说明符仅支持用于 SingleDoubleBigInteger 类型。The round-trip ("R" or "r") format specifier is supported only for Single, Double, and BigInteger types.

下列支持标准数字格式字符串:Standard numeric format strings are supported by:

有关详细信息,请参阅标准数值格式字符串For more information, see Standard Numeric Format Strings.

请参阅See also