c# 标准数字格式字符串
来源:互联网 发布:护肤品牌 知乎 编辑:程序博客网 时间:2024/06/12 18:32
标准数字格式字符串用于格式化通用数值类型。标准数字格式字符串采用 Axx 的形式,其中 A 是称为格式说明符的字母型字符,xx 是称为精度说明符的可选整数。精度说明符的范围从 0 到 99,并且影响结果中的位数。任何包含一个以上字母字符(包括空白)的数字格式字符串都被解释为自定义数字格式字符串。
下表描述了标准数字格式说明符。有关各格式说明符所产生输出的示例,请参见标准数字格式字符串输出示例。有关更多信息,请参见表格之后的备注。
C 或 c
货币
数字转换为表示货币金额的字符串。转换由当前 NumberFormatInfo 对象的货币格式信息控制。
精度说明符指示所需的小数位数。如果省略精度说明符,则使用当前 NumberFormatInfo 对象给定的默认货币精度。
D 或 d
十进制数
只有整型才支持此格式。数字转换为十进制数字 (0-9) 的字符串,如果数字为负,则前面加负号。
精度说明符指示结果字符串中所需的最少数字个数。如果需要的话,则用零填充该数字的左侧,以产生精度说明符给定的数字个数。
E 或 e
科学记数法(指数)
数字转换为“-d.ddd…E+ddd”或“-d.ddd…e+ddd”形式的字符串,其中每个“d”表示一个数字 (0-9)。如果该数字为负,则该字符串以减号开头。小数点前总有一个数字。
精度说明符指示小数点后所需的位数。如果省略精度说明符,则使用默认值,即小数点后六位数字。
格式说明符的大小写指示在指数前加前缀“E”还是“e”。指数总是由正号或负号以及最少三位数字组成。如果需要,用零填充指数以满足最少三位数字的要求。
F 或 f
定点
数字转换为“-ddd.ddd…”形式的字符串,其中每个“d”表示一个数字 (0-9)。如果该数字为负,则该字符串以减号开头。
精度说明符指示所需的小数位数。如果忽略精度说明符,则使用当前 NumberFormatInfo 对象给定的默认数值精度。
G 或 g
常规
根据数字类型以及是否存在精度说明符,数字会转换为定点或科学记数法的最紧凑形式。如果精度说明符被省略或为零,则数字的类型决定默认精度,如下表所示。
Byte 或 SByte:3
Int16 或 UInt16:5
Int32 或 UInt32:10
Int64 或 UInt64:19
Single:7
Double:15
Decimal:29
如果用科学记数法表示数字时指数大于 -5 而且小于精度说明符,则使用定点表示法;否则使用科学记数法。如果要求有小数点,并且忽略尾部零,则结果包含小数点。如果精度说明符存在,并且结果的有效数字位数超过指定精度,则通过舍入删除多余的尾部数字。
上述规则有一个例外:如果数字是 Decimal 而且省略精度说明符时。在这种情况下总使用定点表示法并保留尾部零。
使用科学记数法时,如果格式说明符是“G”,结果的指数带前缀“E”;如果格式说明符是“g”,结果的指数带前缀“e”。
N 或 n
数字
数字转换为“-d,ddd,ddd.ddd…”形式的字符串,其中“-”表示负数符号(如果需要),“d”表示数字 (0-9),“,”表示数字组之间的千位分隔符,“.”表示小数点符号。实际的负数模式、数字组大小、千位分隔符以及十进制分隔符由当前 NumberFormatInfo 对象指定。
精度说明符指示所需的小数位数。如果忽略精度说明符,则使用当前 NumberFormatInfo 对象给定的默认数值精度。
P 或 p
百分比
数字转换为由 NumberFormatInfo.PercentNegativePattern 或 NumberFormatInfo.PercentPositivePattern 属性定义的、表示百分比的字符串,前者用于数字为负的情况,后者用于数字为正的情况。已转换的数字乘以 100 以表示为百分比。
精度说明符指示所需的小数位数。如果忽略精度说明符,则使用当前 NumberFormatInfo 对象给定的默认数值精度。
R 或 r
往返过程
只有 Single 和 Double 类型支持此格式。往返过程说明符保证转换为字符串的数值再次被分析为相同的数值。使用此说明符格式化数值时,首先使用常规格式对其进行测试:Double 使用 15 位精度,Single 使用 7 位精度。如果此值被成功地分析回相同的数值,则使用常规格式说明符对其进行格式化。但是,如果此值未被成功地分析为相同数值,则它这样格式化:Double 使用 17 位精度,Single 使用 9 位精度。
虽然此处可以存在精度说明符,但它将被忽略。使用此说明符时,往返过程优先于精度。
X 或 x
十六进制数
只有整型才支持此格式。数字转换为十六进制数字的字符串。格式说明符的大小写指示对大于 9 的十六进制数字使用大写字符还是小写字符。例如,使用“X”产生“ABCDEF”,使用“x”产生“abcdef”。
精度说明符指示结果字符串中所需的最少数字个数。如果需要的话,则用零填充该数字的左侧,以产生精度说明符给定的数字个数。
任何其他单个字符
(未知说明符)
(未知说明符将引发运行库格式异常。)
备注
控制面板设置
控制面板中“区域和语言选项”项中的设置会影响由格式化操作产生的结果字符串。这些设置用于初始化与当前线程区域性关联的 NumberFormatInfo 对象,并且当前线程区域性将提供用于控制格式设置的值。使用不同设置的计算机会生成不同的结果字符串。
NumberFormatInfo 属性
格式化受当前 NumberFormatInfo 对象的属性影响,其由当前线程区域性隐式提供或由调用格式化的方法的 IFormatProvider 参数显示提供。为该参数指定 NumberFormatInfo 或 CultureInfo 对象。
整型和浮点型数值类型
对标准数字格式说明符的一些说明涉及到整型或浮点型数值类型。整型数值类型有 Byte、SByte、Int16、Int32、Int64、UInt16、UInt32 和 UInt64。浮点型数值类型有 Decimal、Single 和 Double。
浮点型无穷大和 NaN
请注意,无论格式字符串原来是什么值,只要 Single 或 Double 浮点类型的值为正无穷大、负无穷大或非数字 (NaN),格式字符串就分别是当前适用的 NumberFormatInfo 对象指定的 PositiveInfinitySymbol、NegativeInfinitySymbol 或 NaNSymbol 属性的值。
示例
下面的代码示例使用线程当前区域性、指定区域性以及所有标准数字格式说明符对一个整型数值和一个浮点型数值进行格式化。本代码示例使用两种特定数值类型,但对于任一基本数值类型(Byte、SByte、Int16、Int32、Int64、UInt16、UInt32、UInt64、Decimal、Single 和 Double)将产生类似的结果。
// This code example demonstrates the ToString(String) and // ToString(String, IFormatProvider) methods for integral and// floating-point numbers, in conjunction with the standard // numeric format specifiers.// This code example uses the System.Int32 integral type and // the System.Double floating-point type, but would yield // similar results for any of the numeric types. The integral // numeric types are System.Byte, SByte, Int16, Int32, Int64, // UInt16, UInt32, and UInt64. The floating-point numeric types // are Decimal, Single, and Double.using System;using System.Globalization;using System.Threading;class Sample { public static void Main() {// Format a negative integer or floating-point number in various ways. int integralVal = -12345; double floatingVal = -1234.567d; string msgCurrency = "(C) Currency: . . . . . . "; string msgDecimal = "(D) Decimal:. . . . . . . "; string msgScientific = "(E) Scientific: . . . . . "; string msgFixedPoint = "(F) Fixed point:. . . . . "; string msgGeneral = "(G) General (default):. . "; string msgNumber = "(N) Number: . . . . . . . "; string msgPercent = "(P) Percent:. . . . . . . "; string msgRoundTrip = "(R) Round-trip: . . . . . "; string msgHexadecimal = "(X) Hexadecimal:. . . . . "; string msg1 = "Use ToString(String) and the current thread culture.\n"; string msg2 = "Use ToString(String, IFormatProvider) “ + ”and a specified culture.\n"; string msgCulture = "Culture:"; string msgIntegralVal = "Integral value:"; string msgFloatingVal = "Floating-point value:"; CultureInfo ci;// Console.Clear(); Console.WriteLine("Standard Numeric Format Specifiers:\n");// Display the values. Console.WriteLine(msg1);// Display the thread current culture, which is used to format the values. ci = Thread.CurrentThread.CurrentCulture; Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);// Display the integral and floating-point values. Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal); Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal); Console.WriteLine();// Use the format specifiers that are only for integral types. Console.WriteLine("Format specifiers only for integral types:"); Console.WriteLine(msgDecimal + integralVal.ToString("D")); Console.WriteLine(msgHexadecimal + integralVal.ToString("X")); Console.WriteLine();// Use the format specifier that is only for the Single and Double // floating-point types. Console.WriteLine("Format specifier only for the Single and Double types:"); Console.WriteLine(msgRoundTrip + floatingVal.ToString("R")); Console.WriteLine();// Use the format specifiers that are for integral or floating-point types. Console.WriteLine("Format specifiers for integral or floating-point types:"); Console.WriteLine(msgCurrency + floatingVal.ToString("C")); Console.WriteLine(msgScientific + floatingVal.ToString("E")); Console.WriteLine(msgFixedPoint + floatingVal.ToString("F")); Console.WriteLine(msgGeneral + floatingVal.ToString("G")); Console.WriteLine(msgNumber + floatingVal.ToString("N")); Console.WriteLine(msgPercent + floatingVal.ToString("P")); Console.WriteLine();// Display the same values using a CultureInfo object. The CultureInfo class // implements IFormatProvider. Console.WriteLine(msg2);// Display the culture used to format the values. // Create a European culture and change its currency symbol to "euro" because // this particular code example uses a thread current UI culture that cannot // display the euro symbol (€). ci = new CultureInfo("de-DE"); ci.NumberFormat.CurrencySymbol = "euro"; Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);// Display the integral and floating-point values. Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal); Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal); Console.WriteLine();// Use the format specifiers that are only for integral types. Console.WriteLine("Format specifiers only for integral types:"); Console.WriteLine(msgDecimal + integralVal.ToString("D", ci)); Console.WriteLine(msgHexadecimal + integralVal.ToString("X", ci)); Console.WriteLine();// Use the format specifier that is only for the Single and Double // floating-point types. Console.WriteLine("Format specifier only for the Single and Double types:"); Console.WriteLine(msgRoundTrip + floatingVal.ToString("R", ci)); Console.WriteLine();// Use the format specifiers that are for integral or floating-point types. Console.WriteLine("Format specifiers for integral or floating-point types:"); Console.WriteLine(msgCurrency + floatingVal.ToString("C", ci)); Console.WriteLine(msgScientific + floatingVal.ToString("E", ci)); Console.WriteLine(msgFixedPoint + floatingVal.ToString("F", ci)); Console.WriteLine(msgGeneral + floatingVal.ToString("G", ci)); Console.WriteLine(msgNumber + floatingVal.ToString("N", ci)); Console.WriteLine(msgPercent + floatingVal.ToString("P", ci)); Console.WriteLine(); }}/*This code example produces the following results:Standard Numeric Format Specifiers:Use ToString(String) and the current thread culture.Culture: English (United States)Integral value: -12345Floating-point value: -1234.567Format specifiers only for integral types:(D) Decimal:. . . . . . . -12345(X) Hexadecimal:. . . . . FFFFCFC7Format specifier only for the Single and Double types:(R) Round-trip: . . . . . -1234.567Format specifiers for integral or floating-point types:(C) Currency: . . . . . . ($1,234.57)(E) Scientific: . . . . . -1.234567E+003(F) Fixed point:. . . . . -1234.57(G) General (default):. . -1234.567(N) Number: . . . . . . . -1,234.57(P) Percent:. . . . . . . -123,456.70 %Use ToString(String, IFormatProvider) and a specified culture.Culture: German (Germany)Integral value: -12345Floating-point value: -1234.567Format specifiers only for integral types:(D) Decimal:. . . . . . . -12345(X) Hexadecimal:. . . . . FFFFCFC7Format specifier only for the Single and Double types:(R) Round-trip: . . . . . -1234,567Format specifiers for integral or floating-point types:(C) Currency: . . . . . . -1.234,57 euro(E) Scientific: . . . . . -1,234567E+003(F) Fixed point:. . . . . -1234,57(G) General (default):. . -1234,567(N) Number: . . . . . . . -1.234,57(P) Percent:. . . . . . . -123.456,70%*/
- c# 标准数字格式字符串
- C#标准数字格式字符串输出示例
- 标准数字格式字符串
- 标准数字格式字符串
- 标准数字格式字符串
- 【整理】C# ToString格式字符串整理(Format)(数字、日期和枚举的标准格式设置说明符)(SamWang)
- C# 标准日期和时间格式字符串
- C# 标准日期和时间格式字符串
- C# 自定义数字格式字符串 ToString ###,###,###,##0
- 标准数字格式字符串用法说明及举例
- 标准数字格式字符串用法说明及举例
- 标准数字格式字符串用法说明及举例
- Formatting Types 标准及自定义数字格式字符串
- C# 的数据类型转换 c#字符串转换格式 c#日期数字字符串整形格式转换
- 标准 DateTime 格式字符串
- 标准 DateTime 格式字符串
- 数字格式字符串
- 自定义数字格式字符串
- ControlTemplate in WPF —— DataGrid
- 处理【Fatal error: Can't use function return value in write context 】的情况之《empty的解析》
- 苹果iOS 6正式上线:Siri支持中文 内置苹果地图
- Linux内核之进程间通讯
- error C2733: 不允许重载函数“DllGetClassObject”的第二个 C 链接
- c# 标准数字格式字符串
- Linux内核之PCI设备
- 二值图像连通域标记算法与代码
- Linux内核之中断系统
- RPC学习笔记
- 将排序数组转为平衡二叉查找树
- 动态JS
- div层 被 flash 层 遮挡的解决方法
- Linux内核之设备驱动