printf()的格式控制字符

一、printf( )的格式字符:

① d格式符 用来输出十进制整数
%d 按整型数据的实际长度输出
读作mod d。

%md 使输出长度为m,如果数据长度小于m,则左补空格,如果大于m,则输出实际长度
%ld 输出长整型数据
② o格式符 以八进制形式输出整数
③ x格式符 以十六进制形式输出整数
④ u格式符 用来输出unsigned型数据,以十进制形式输出
⑤ c格式符 用来输出一个字符
⑥ s格式符 输出一个字符串
%s 输出实际长度字符串
%ms 输出的串占m列,如果串长度小于m,左补空格,如果大于m,实际输出
%-ms输出的串占m列,如果串长度小于m,右补空格,
%m.ns 输出占m列,但只取字符串中左端n个字符并靠右对齐
%-m.ns m、n含义同上,靠左对齐,如果n>m,则m自动取n值
⑦ f格式符 以小数形式输出实数
%f 整数部分全部输出,小数部分输出6位
%m.nf 输出数据共占m列,其中有n位小数。如果数值长度小于m,左补空格
%-m.nf 同上,右补空格
⑧ e格式符 以指数形式输出实数
%e 系统指定6位小数,5位指数(e+002 )
⑨ g格式符 输出实数,根据数值大小,自动选f格式或e格式


二、JAVA常用的格式控制和转换字符


3.2.4 输出格式控制

标准输出通常是使用命令行模式作为输出,这里介绍几个输出格式控制技巧,在命令行模式显示时可以协助控制输出的格式。首先介绍格式控制字符,常用的控制字符如表3-1所示。

控 制 字 符 作 用

\ 反斜线

\' 单引号'

\" 双引号"

\uxxxx 以十六进制数指定Unicode字符输出

\dxxx 以八进制数指定Unicode字符输出

\b 倒退一个字符

\f 换页

\n 换行

\r 光标移至行首

\t 跳格(一个Tab键)







表3-2 常用格式转换字符

转换字符 作 用



%% 在字符串中显示%

%d 以十进制整数方式输出,提供的数必须是Byte、Short、 Integer、Long或BigInteger

%f 将浮点数以十进制方式输出,提供的数必须是Float、Double或 BigDecimal

%e, %E 将浮点数以十进制方式输出,并使用科学记数法,提供的数必须是Float、 Double或BigDecimal

%a, %A 使用科学记数法输出浮点数,以十六进制输出整数部分,以十进制输出指数部分,提供的数必须Float、Double、
BigDecimal

%o 以八进制整数方式输出,提供的数必须是Byte、Short、Integer、Long或BigInteger

%x, %X 将浮点数以十六进制方式输出,提供的数必须是Byte、Short、Integer、Long、或BigInteger

%s, %S 将字符串格式化输出

%c, %C 以字符方式输出,提供的数必须是Byte、Short、Character或 Integer

%b, %B 将true或false输出(或TRUE、FALSE,使用 %B)。另外,非null值输出是true,null值输出是false

%t, %T 输出日期/时间的前置,详情请看在线API文件


三、.NET Framework 开发人员指南

标准数字格式字符串

标准数字格式字符串用于格式化通用数值类型。标准数字格式字符串采用 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)将产生类似的结果。

C#
复制代码

// 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: -12345
Floating-point value: -1234.567

Format specifiers only for integral types:
(D) Decimal:. . . . . . . -12345
(X) Hexadecimal:. . . . . FFFFCFC7

Format specifier only for the Single and Double types:
(R) Round-trip: . . . . . -1234.567

Format 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: -12345
Floating-point value: -1234.567

Format specifiers only for integral types:
(D) Decimal:. . . . . . . -12345
(X) Hexadecimal:. . . . . FFFFCFC7

Format specifier only for the Single and Double types:
(R) Round-trip: . . . . . -1234,567

Format 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%

*/

请参见
参考
NumberFormatInfo
概念
数字格式字符串
标准数字格式字符串输出示例
自定义数字格式字符串
其他资源
格式化类型
[url]http://msdn.microsoft.com/zh-cn/dwhawy9k(VS.80).aspx[/url]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值