C# String.Format格式说明

C#格式化数值结果表

字符

说明

示例

输出

C货币string.Format("{0:C3}", 2)$2.000
D十进制string.Format("{0:D3}", 2)002
E科学计数法1.20E+0011.20E+001
G常规string.Format("{0:G}", 2)2
N用分号隔开的数字string.Format("{0:N}", 250000)250,000.00
X十六进制string.Format("{0:X000}", 12)C
  string.Format("{0:000.000}", 12.2)012.200

Strings

There really isn't any formatting within a strong, beyond it's alignment. Alignment works for any argument being printed in a String.Format call.

 

SampleGenerates
String.Format("->{1,10}<-", "Hello");-> Hello<-
String.Format("->{1,-10}<-", "Hello");->Hello <-

Numbers

Basic number formatting specifiers:

 

SpecifierTypeFormat

Output 
(Passed 
Double 1.42)

Output 
(Passed 
Int -12400)

cCurrency{0:c}$1.42-$12,400
dDecimal (Whole number){0:d}System.
FormatException
-12400
eScientific{0:e}1.420000e+000-1.240000e+004
fFixed point{0:f}1.42-12400.00
gGeneral{0:g}1.42-12400
nNumber with commas for thousands{0:n}1.42-12,400
rRound trippable{0:r}1.42System.
FormatException
xHexadecimal{0:x4}System.
FormatException
cf90

Custom number formatting:

 

SpecifierTypeExampleOutput (Passed Double 1500.42)Note
0Zero placeholder{0:00.0000}1500.4200Pads with zeroes.
#Digit placeholder{0:(#).##}(1500).42 
.Decimal point{0:0.0}1500.4 
,Thousand separator{0:0,0}1,500Must be between two zeroes.
,.Number scaling{0:0,.}2Comma adjacent to Period scales by 1000.
%Percent{0:0%}150042%Multiplies by 100, adds % sign.
eExponent placeholder{0:00e+0}15e+2Many exponent formats available.
;Group separatorsee below  

The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses. This currency formatting example at the bottom of this document makes it obvious:

Dates

Note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale.

 

SpecifierTypeExample (Passed System.DateTime.Now)
dShort date10/12/2002
DLong dateDecember 10, 2002
tShort time10:11 PM
TLong time10:11:29 PM
fFull date & timeDecember 10, 2002 10:11 PM
FFull date & time (long)December 10, 2002 10:11:29 PM
gDefault date & time10/12/2002 10:11 PM
GDefault date & time (long)10/12/2002 10:11:29 PM
MMonth day patternDecember 10
rRFC1123 date stringTue, 10 Dec 2002 22:11:29 GMT
sSortable date string2002-12-10T22:11:29
uUniversal sortable, local time2002-12-10 22:13:50Z
UUniversal sortable, GMTDecember 11, 2002 3:13:50 AM
YYear month patternDecember, 2002

The 'U' specifier seems broken; that string certainly isn't sortable.

Custom date formatting:

 

SpecifierTypeExampleExample Output
ddDay{0:dd}10
dddDay name{0:ddd}Tue
ddddFull day name{0:dddd}Tuesday
f, ff, ...Second fractions{0:fff}932
gg, ...Era{0:gg}A.D.
hh2 digit hour{0:hh}10
HH2 digit hour, 24hr format{0:HH}22
mmMinute 00-59{0:mm}38
MMMonth 01-12{0:MM}12
MMMMonth abbreviation{0:MMM}Dec
MMMMFull month name{0:MMMM}December
ssSeconds 00-59{0:ss}46
ttAM or PM{0:tt}PM
yyYear, 2 digits{0:yy}02
yyyyYear{0:yyyy}2002
zzTimezone offset, 2 digits{0:zz}-05
zzzFull timezone offset{0:zzz}-05:00
:Separator{0:hh:mm:ss}10:43:20
/Separator{0:dd/MM/yyyy}10/12/2002

Enumerations

 

SpecifierType
gDefault (Flag names if available, otherwise decimal)
fFlags always
dInteger always
xEight digit hex.

Some Useful Examples

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);

This will output "$1,240.00" if passed 1243.50. It will output the same format but in parentheses if the number is negative, and will output the string "Zero" if the number is zero.

String.Format("{0:(###) ###-####}", 18005551212);

This will output "(800) 555-1212".

 

变量.ToString()


字符型转换 转为字符串 
12345.ToString("n"); //生成 12,345.00 
12345.ToString("C"); //生成 ¥12,345.00 
12345.ToString("e"); //生成 1.234500e+004 
12345.ToString("f4"); //生成 12345.0000 
12345.ToString("x"); //生成 3039 (16进制) 
12345.ToString("p"); //生成 1,234,500.00%

转载于:https://www.cnblogs.com/wwb0111/archive/2012/05/28/3098931.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/434515.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux添加网站后无法显示,Linux:SElinux导致网站无法访问

通过更改SElinux状态可以判断出&#xff0c;当SElinux处于关闭状态时&#xff0c;网站内容访问正常。[rootmaster1-192-168-117-18 ~]# setenforce 0[rootmaster1-192-168-117-18 ~]# getenforcePermissive[rootmaster1-192-168-117-18 ~]# setenforce 1[rootmaster1-192-168-1…

Linux rpm 命令参数使用详解[介绍和应用]

RPM是RedHat Package Manager&#xff08;RedHat软件包管理工具&#xff09;类似Windows里面的“添加/删除程序” rpm 执行安装包 二进制包&#xff08;Binary&#xff09;以及源代码包&#xff08;Source&#xff09;两种。二进制包可以直接安装在计算机中&#xff0c;而源代…

VS快捷键和小功能

转自&#xff1a;http://www.cnblogs.com/wangshenhe/archive/2012/05/28/2521393.html备份一个 VS隐藏的快捷键和小功能 可将代码放入工具箱&#xff0c;类似控件使用。CtrlShiftV:剪贴板循环粘贴。CtrlT:变换一个字符&#xff0c;即将字符后移。CtrlShiftT:变换一个词&#x…

linux 分区 文件,Linux的分区与文件结构

Linux操作系统与我们经常使用的windows操作系统有所不同&#xff0c;Linux主机上的设备以及系统的分区都以文件的形式存在着。接下来就将Linux系统中设备以及分区的标识方法以及目录结构做以详细介绍。在Linux系统中主要以接口类型来区分不同的存储设备:IDE接口用hd来表示&…

CentOS卸载OpenJDK并安装Sun JDK

第一步&#xff1a;查看Linux自带的JDK是否已安装 &#xff08;卸载centOS已安装的1.4&#xff09; 安装好的CentOS会自带OpenJdk,用命令 java -version &#xff0c;会有下面的信息&#xff1a; java version "1.6.0" OpenJDK Runtime Environment (build 1.6.0-b09…

AndroidManifest.xml文件详解(uses-sdk)

语法&#xff08;SYNTAX&#xff09;&#xff1a; <uses-sdkandroid:minSdkVersion"integer" android:targetSdkVersion"integer" android:maxSdkVersion"integer"/> 被包含于&#xff08;CONTAINED IN&#xff09;&a…

xps13 linux 硬盘分区,在xps13上安装ubuntu16.04教程

在windows界面下&#xff0c;通过磁盘管理器&#xff0c;从硬盘中分出40G的空闲空间。重启电脑&#xff0c;进入BIOS&#xff0c;将启动模式修改为ACHI(如果想系统安装成功后可以直接使用无线wifi&#xff0c;请将security boot关闭&#xff1b;否则&#xff0c;默认只能使用有…

Ubuntu10.10的网络配置

有一阵子着实对Ubuntu的网络配置很迷惑&#xff0c;耐下心来仔细上网找了找&#xff0c;有点小心得&#xff0c;总结一下。 先说下大概的配置过程&#xff0c;再去细究一些情况。 一、配置大概分三类&#xff1a;通过配置文件配置、通过命令配置、通过图形化的网络连接菜单配置…

sp_executesql介绍和使用 转

转自http://www.cnblogs.com/wanyuan8/archive/2011/11/09/2243483.htmlexecute相信大家都用的用熟了&#xff0c;简写为exec,除了用来执行存储过程&#xff0c;一般都用来执行动态Sql sp_executesql&#xff0c;sql2005中引入的新的系统存储过程&#xff0c;也是用来处理动态s…

linux多网卡udp组播收不到数,UDP组播,完成端口,双网卡收不到数据?帮帮忙

当前位置:我的异常网 网络通信 UDP组播&#xff0c;完成端口&#xff0c;双网卡收不到数据&#xff1f;帮帮忙UDP组播&#xff0c;完成端口&#xff0c;双网卡收不到数据&#xff1f;帮帮忙www.myexceptions.net 网友分享于&#xff1a;2013-04-13 浏览&#xff1a;848次UDP…

DllMain详解

1 DLL的进入/退出函数 1.1 DllMain简介 跟exe有个main或者WinMain入口函数一样&#xff0c;DLL也有一个入口函数&#xff0c;就是DllMain。以“DllMain”为关键字&#xff0c;来看看MSDN帮助文档怎么介绍这个函数的。 The DllMain function is an optional method of entr…

linux 视频学习

Linux 系统管理员要求对系统进行管理&#xff0c;备份等操作&#xff0c;linux程序员需要掌握c,c,java,php,jsp等 Linux平台上的开发&#xff0c;包括vi,gcc,gdb,make,jdk,tomcat,mysql… 书籍介绍&#xff1a;鸟哥LINUX私房菜&#xff0c;LINUX编程从入门到精通&#xff0c;li…

linux安装tensorflow教程,真正从零开始,TensorFlow详细安装入门图文教程!

在正式开始之前我想说&#xff1a;一定要注意窗口给出的提示(英文)。在实际操作中可能会碰到各种各样的问题&#xff0c;但常见的问题其实都可以根据它的报错信息找到原因&#xff0c;只要上网搜一搜相应的信息就能解决&#xff0c;甚至它自己就会给出解决的建议。如果你发现你…

引路蜂地图API:Gis.Navigation包定义

本包提供了路口到路口实时导航API&#xff0c;从地图服务器返回的路径信息含有文字和路径的地理坐标信息&#xff0c;类NavigationEngine根据路径和当前坐标实现实时导航。它内部含用三个工作线程&#xff1a; Location Monitor 实时监视当前位置坐标是否偏离路径&#xff0c;如…

ubuntu下安装opensips

1. 下载opensips1.8版本&#xff0c;并解压到 "/usr/local/src" 目录下 &#xff1b;2. 安装必要的软件包&#xff0c;apt-get install flex bison libncurses-dev3. 进入opensips源码目录&#xff0c;make menuconfig出现opensips的配置界面后1) 选择"Configur…

linux 修改Db2主机名,修改DB2服务器的主机名

环境:产品&#xff1a;DB2 UDB平台&#xff1a;AIX&#xff0c;Solaris&#xff0c;HP-UX&#xff0c;Linux&#xff0c;Windows版本&#xff1a;V8 V9.1V9.5为了修改服务器的主机名&#xff0c;我们可以在DB2数据库服务器上执行以下步骤来实现&#xff1a;1) 停止DB2管理服务器…

用OpenMP加速你的程序[转]

最近在看多核编程。简单来说&#xff0c;由于现在电脑CPU一般都有两个核&#xff0c;4核与8核的CPU也逐渐走入了寻常百姓家&#xff0c;传统的单线程编程方式难以发挥多核 CPU的强大功能&#xff0c;于是多核编程应运而生。按照我的理解&#xff0c;多核编程可以认为是对多线程…

错误./hello: error while loading shared libraries: libQtGui.so.4: cannot open shared object file:

之前一直想在ARM 上跑qt&#xff0c;但都出现错误&#xff1a; ./hello: error while loading shared libraries: libQtGui.so.4: cannot open shared object file: No such file or directory 这主要是ARM 上的运行环境设置不当&#xff1a; 我用的是飞凌的6410 环境变量设…

linux 指令引用变量,Linux之变量引用与命令替换

在bash脚本编写中&#xff0c;我们时常需要引用变量与替换命令&#xff0c;为规范操作&#xff0c;现对其做简单的总结说明。引用引用就是指将字符串用引用符号括起来&#xff0c;以防止特殊字符被shell脚本解释为其他意义。引用时屏蔽特殊字符的特殊意义&#xff0c;而将其解释…

有些垃圾网站转载都不会

有时会看到我的文章被转载&#xff0c;只要保留作者信息和原文链接&#xff0c;并且忠实于原文都是很欢迎的。这里的忠实原文应该是最基本的了吧&#xff0c;转载嘛&#xff0c;最简单的也就是拷贝粘贴吧&#xff0c;可发现有些垃圾网站&#xff0c;连拷贝粘贴都做不好&#xf…