`
黄继华
  • 浏览: 45056 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

C#格式化数值结果表

 
阅读更多

C#格式化数值结果表
字符
说明
示例
输出
C 货币 string.Format("{0:C3}", 2) $2.000
D 十进制 string.Format("{0:D3}", 2) 002
E 科学计数法 1.20E+001 1.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.
Sample Generates
String.Format("->{1,10}<-", "Hello"); -> Hello<-
String.Format("->{1,-10}<-", "Hello"); ->Hello <-
Numbers
Basic number formatting specifiers:
Specifier Type Format
Output
(Passed
Double 1.42)
Output
(Passed
Int -12400)
c Currency {0:c} $1.42 -$12,400
d Decimal (Whole number) {0:d} System.
FormatException
-12400
e Scientific {0:e} 1.420000e+000 -1.240000e+004
f Fixed point {0:f} 1.42 -12400.00
g General {0:g} 1.42 -12400
n Number with commas for thousands {0:n} 1.42 -12,400
r Round trippable {0:r} 1.42 System.
FormatException
x Hexadecimal {0:x4} System.
FormatException
cf90
Custom number formatting:
Specifier Type Example Output (Passed Double 1500.42) Note
0 Zero placeholder {0:00.0000} 1500.4200 Pads with zeroes.
# Digit placeholder {0:(#).##} (1500).42
. Decimal point {0:0.0} 1500.4
, Thousand separator {0:0,0} 1,500 Must be between two zeroes.
,. Number scaling {0:0,.} 2 Comma adjacent to Period scales by 1000.
% Percent {0:0%} 150042% Multiplies by 100, adds % sign.
e Exponent placeholder {0:00e+0} 15e+2 Many exponent formats available.
; Group separator see 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.
Specifier Type Example (Passed System.DateTime.Now)
d Short date 10/12/2002
D Long date December 10, 2002
t Short time 10:11 PM
T Long time 10:11:29 PM
f Full date & time December 10, 2002 10:11 PM
F Full date & time (long) December 10, 2002 10:11:29 PM
g Default date & time 10/12/2002 10:11 PM
G Default date & time (long) 10/12/2002 10:11:29 PM
M Month day pattern December 10
r RFC1123 date string Tue, 10 Dec 2002 22:11:29 GMT
s Sortable date string 2002-12-10T22:11:29
u Universal sortable, local time 2002-12-10 22:13:50Z
U Universal sortable, GMT December 11, 2002 3:13:50 AM
Y Year month pattern December, 2002
The 'U' specifier seems broken; that string certainly isn't sortable.
Custom date formatting:
Specifier Type Example Example Output
dd Day {0:dd} 10
ddd Day name {0:ddd} Tue
dddd Full day name {0:dddd} Tuesday
f, ff, ... Second fractions {0:fff} 932
gg, ... Era {0:gg} A.D.
hh 2 digit hour {0:hh} 10
HH 2 digit hour, 24hr format {0:HH} 22
mm Minute 00-59 {0:mm} 38
MM Month 01-12 {0:MM} 12
MMM Month abbreviation {0:MMM} Dec
MMMM Full month name {0:MMMM} December
ss Seconds 00-59 {0:ss} 46
tt AM or PM {0:tt} PM
yy Year, 2 digits {0:yy} 02
yyyy Year {0:yyyy} 2002
zz Timezone offset, 2 digits {0:zz} -05
zzz Full timezone offset {0:zzz} -05:00
: Separator {0:hh:mm:ss} 10:43:20
/ Separator {0:dd/MM/yyyy} 10/12/2002
Enumerations
Specifier Type
g Default (Flag names if available, otherwise decimal)
f Flags always
d Integer always
x Eight 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%
分享到:
评论

相关推荐

    C#格式化数值结果表.doc

    在C#编程中,格式化数值结果是至关重要的,它能帮助我们以更易读、易理解的方式展示数据。在本文档中,我们将深入探讨C#中的几种主要的数值格式化方式,包括基本的和自定义的格式。 1. **基本格式化** - **C**: ...

    C#格式化字符串C#格式化字符串

    本文详细介绍了C#中字符串格式化的方法及应用场景,包括基本格式化、数值格式化以及指定精度等。掌握这些技巧对于编写高质量的C#应用程序至关重要。通过合理的字符串格式化,可以极大地提升用户界面的友好性和程序的...

    C#数值格式化

    在C#编程中,数值格式化是一个非常重要的概念,它允许程序员按照特定的样式和精度显示数字。在本文中,我们将深入探讨C#中的数值格式化,包括基本格式字符串、自定义模式输出以及一些常见的使用示例。 1. **基本...

    C#格式化字符串讲解

    `String.Format`是C#中最常用的字符串格式化方法之一,它允许我们将变量或表达式的结果插入到字符串模板中。其基本语法如下: ```csharp string result = String.Format("模板字符串", 参数1, 参数2, ...); ``` ...

    C#实现格式化数据功能

    二、数值格式化 C#提供了一系列的`System.Globalization.NumberFormatInfo`类的实例属性来控制数字的显示方式,如千位分隔符、小数点位置等。例如: ```csharp decimal price = 12345.67m; string priceFormatted ...

    C#中字符串的格式化及转换成数值的方法

    ### C#中字符串的格式化及转换成数值的方法 在C#编程中,字符串的处理是非常常见且重要的任务之一。本文将详细介绍如何在C#中进行字符串的格式化以及如何将字符串转换为数值类型,包括整数、浮点数等。 #### 一、...

    c#格式化数字

    在C#中,格式化数字是一项非常实用的功能,它能够帮助开发者按照特定的要求来展示数值。根据提供的代码示例,我们可以总结出以下关于C#中数字格式化的关键知识点: ### 1. 使用`string.Format()`方法进行格式化 `...

    C#t中有关tostring函数的格式

    数值格式化: * C:货币格式,例如:2.5.ToString("C")¥2.50 * D:十进制数格式,例如:25.ToString("D5")00025 * E:科学型格式,例如:25000.ToString("E")2.500000E+005 * F:固定点格式,例如:25.ToString(...

    C# Tostring 格式化输出字符串全解

    `2.5.ToString("C")` 结果为 "¥2.50",这表明使用"C"格式化代码可以将数值转换为货币格式,通常包括货币符号和两位小数。 2. 十进制(D)格式 如 `25.ToString("D5")` 结果为 "00025","D"后跟的数字代表最小的...

    C#String.Format数字格式化输出 .txt

    在C#编程语言中,`String.Format`方法是一种强大的字符串格式化工具,它允许开发者以预定义的格式输出各种类型的数据,特别是在处理数字时。通过使用特定的格式化字符串,可以控制数字的显示方式,包括小数点后的...

    C#时间格式化函数

    C#函数代码,1.数值转换为固定hh:mm:ss格式,2.//获取当前时间yyyy-MM-dd hh:mm:ss,3.2018-06-26T16:30:04.89→yyyy-MM-dd hh:mm:ss 进行格式化

    c#格式化字符串.pdf

    C#中的字符串格式化是一个强大的功能,用于创建和定制输出字符串。`String.Format`方法是C#中常用的一种格式化字符串的方式,它允许你插入变量并控制它们的显示方式。在MFC、WTL和STL中也有类似的字符串格式化方法,...

    C#实现的数值文本框(TextBox)组件TDecEditBox

    通过这样的自定义控件,开发者可以更方便地在C#应用程序中处理数值输入,避免了在代码中编写大量验证和格式化逻辑,提高了代码的可读性和维护性。同时,这种组件化的设计也遵循了软件开发的复用原则,提升了开发效率...

    c#格式化字符串.docx

    C#中的字符串格式化是一个强大的功能,它允许程序员根据特定的规范来构造和输出复杂的字符串。在C#中,`String.Format`方法和`Console.WriteLine`都是常用的字符串格式化手段,它们都支持传递多个参数,并根据指定的...

    c#DataGridView数据绑定示例 格式化单元格的内容源码

    本示例主要关注如何将数据绑定到`DataGridView`并格式化单元格内容,这对于创建用户友好的界面和提升数据可视化效果至关重要。 首先,数据绑定是将数据源(如数据库、数组或集合)与`DataGridView`关联的过程,使得...

    C#自制数值文本框组件

    3. **格式化显示**:组件可能需要根据用户设置自动格式化数值,比如保留小数位数、使用千分符等。这可以通过自定义的属性(如`DecimalPlaces`、`UseThousandSeparator`)控制,并在`Paint`事件中更新显示内容。 4. ...

    C#String.Format数字格式化

    ### C#中的String.Format方法与数字格式化 在C#编程语言中,`String.Format`是一种非常实用且灵活的方法,用于格式化字符串输出。通过该方法可以方便地控制数字、日期时间等数据类型的显示格式,这在实际开发过程中...

    C# Winform数值实时曲线(完整示例)

    总结来说,"C# Winform数值实时曲线"项目涵盖了实时数据处理、图形绘制、用户交互、数据持久化等多个重要知识点。通过学习和实践这个示例,开发者可以掌握在C# Winform应用中构建实时曲线显示功能的关键技术。

Global site tag (gtag.js) - Google Analytics