C#中ToString格式大全
更多资源:http://denghejun.github.io
C |
货币 |
2.5.ToString("C") |
¥2.50 |
D |
十进制数 |
25.ToString("D5") |
00025 |
E |
科学型 |
25000.ToString("E") |
2.500000E+005 |
F |
固定点 |
25.ToString("F2") |
25.00 |
G |
常规 |
2.5.ToString("G") |
2.5 |
N |
数字 |
2500000.ToString("N") |
2,500,000.00 |
X |
十六进制 |
255.ToString("X") |
FF |
formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)
必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。
常用格式举例:
(1) int i=12345;
this.textBox1.Text=i.ToString();
//结果 12345(this指当前对象,或叫当前类的实例)
this.textBox2.Text=i.ToString("d8");
//结果 00012345
(2) int i=123;
double j=123.45;
string s1=string.Format("the value is {0,7:d}",i);
string s2=string.Format("the value is {0,7:f3}",j);
this.textBox1.Text=s1 ;
//结果 the value is 123
this.textBox2.Text=s2;
//结果 the value is 123.450
(3)double i=12345.6789;
this.textBox1.Text=i.ToString("f2"); //结果 12345.68
this.textBox2.Text=i.ToString("f6");
//结果 12345.678900
(4)double i=12345.6789;
this.textBox1.Text=i.ToString("n"); //结果 12,345.68
this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789
(5)double i=0.126;
string s=string.Format("the value is {0:p}",i);
this.textBox1.Text=i.ToString("p"); //结果 12.6%
this.textBox2.Text=s; //结果 the value is 12.6%
(6) DateTime dt =new DateTime(2003,5,25);
this.textBox1.Text=dt.ToString("yy.M.d");
//结果 03.5.25
this.textBox2.Text=dt.ToString(“yyyy年M月”);
//结果 2003年5月
Convert.ToDateTime("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
"2005/12/22 22:22:22"
(7) int i=123;
double j=123.45;
string s=string.Format("i:{0,-7},j:{1,7}",i,j);
//-7表示左对齐,占7位
this.textBox1.Text=s ;
//结果i:123 ,j: 123.45
Byte.ToString 方法 (String, IFormatProvider)的C#例子
下面的代码示例使用 ToString 方法的几个重载格式化 Byte 值。
// Example for the Byte.ToString( ) methods.
using System;
using System.Globalization;
class ByteToStringDemo
{
static void RunToStringDemo( )
{
byte smallValue = 13;
byte largeValue = 234;
// Format the Byte values without and with format strings.
Console.WriteLine( "/nIFormatProvider is not used:" );
Console.WriteLine( " {0,-20}{1,10}{2,10}",
"No format string:", smallValue.ToString( ),
largeValue.ToString( ) );
Console.WriteLine( " {0,-20}{1,10}{2,10}",
"'X2' format string:", smallValue.ToString( "X2" ),
largeValue.ToString( "X2" ) );
// Get the NumberFormatInfo object from the
// invariant culture.
CultureInfo culture = new CultureInfo( "" );
NumberFormatInfo numInfo = culture.NumberFormat;
// Set the digit grouping to 1, set the digit separator
// to underscore, and set decimal digits to 0.
numInfo.NumberGroupSizes = new int[ ] { 1 };
numInfo.NumberGroupSeparator = "_";
numInfo.NumberDecimalDigits = 0;
// Use the NumberFormatInfo object for an IFormatProvider.
Console.WriteLine(
"/nA NumberFormatInfo object with digit group " +
"size = 1 and /ndigit separator " +
"= '_' is used for the IFormatProvider:" );
Console.WriteLine( " {0,-20}{1,10}{2,10}",
"No format string:", smallValue.ToString( numInfo ),
largeValue.ToString( numInfo ) );
Console.WriteLine( " {0,-20}{1,10}{2,10}",
"'N' format string:",
smallValue.ToString( "N", numInfo ),
largeValue.ToString( "N", numInfo ) );
}
static void Main( )
{
Console.WriteLine( "This example of/n" +
" Byte.ToString( ),/n" +
" Byte.ToString( String ),/n" +
" Byte.ToString( IFormatProvider ), and/n" +
" Byte.ToString( String, IFormatProvider )/n" +
"generates the following output when formatting " +
"Byte values /nwith combinations of format " +
"strings and IFormatProvider." );
RunToStringDemo( );
}
}
/*
This example of
Byte.ToString( ),
Byte.ToString( String ),
Byte.ToString( IFormatProvider ), and
Byte.ToString( String, IFormatProvider )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.
IFormatProvider is not used:
No format string: 13 234
'X2' format string: 0D EA
A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
No format string: 13 234
'N' format string: 1_3 2_3_4
*/
C#中ToString格式大全的更多相关文章
- C#.ToString()格式大全
原文:C#.ToString()格式大全 C#.ToString()格式大全 stringstr1=string.Format("{0:N1}",56789); ...
- C#中的ToString格式大全
// C# 日期格式 DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString() ...
- C#中ToString()格式详解
以下内容均摘自博客园,仅供资料查询. ToString格式化 在很多对象显示为字符串的时候都会使用到ToString中的格式化,由于以前没怎么注意到这个问题,想总结一下各个基础结构对象的格式化,以便后 ...
- c# ToString()格式大全(转)
stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0stringstr2 =str ...
- C# ToString格式大全
C# 货币 2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); // 25000 // E ...
- C#中ToString和Formate格式大全
C#中ToString格式大全 stringstr1 =); //result: 56,789.0 stringstr2 =); //result: 56,789.00 stringstr3 =); ...
- Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全
通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...
- Word文档中的格式标记大全
在Word中有很多的格式设置,很多格式设置都会有一些标记,这些标记是隐藏的,在打印文档时是不会打印出来的,但是它们却起着结构化文档的大作用.如果你在编辑文档,不妨点击格式标记开关,看看都有哪些格式标记 ...
- C#时间日期格式大全
C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2016-7-1 14:33:34 如果要换成成201607,07-2016,2016-7- ...
随机推荐
- javase-->基础知识(二)
1.算术运算符 7种:+,-,*,/,%,++,--; --------------------注意不要想当然,按照类型规则来计算 ++在变量前,先加1再取变量使用 --同理 在变量 ...
- linux学习日记之目录配制
linux目录管理遵循FHS标准,主要目标是希望让使用者可以了解已安装软件通常放置于哪个目录上,所以他们希望独立的软件开发商.操作系统制作者.以及想要维护系统的用户,都遵循FHS的标准.也就是说FHS ...
- 记一次proc_open没有开启心得感悟
引言: 今天在部署服务器的时候,使用composer来安装依赖.遇到了 The Process class relies on proc_open, which is not available on ...
- .NET 程序集Assembly使用
概述 一直以来,我们都在用C#编写程序,编写程序的时候,我们用到继承.多态.接口以及泛型,我们也都明白子类可以继承抽象类,并能够重写父类的抽象方法,可是大家是否想过,如下几个问题: 1.凡树必有根和叶 ...
- android基础知识进阶
1.android Activity的生命周期 http://blog.csdn.net/hpoi/article/details/4629065 2.android Service的生命周期 htt ...
- C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)
用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法: 1):通过QrCodeNet(Gma.QrCodeNet.Encoding.dll)来实现 1.1):首先 ...
- windows下指定格式文件转移
#放在目录下执行 1.bat 作用:将该目录下所有mp4格式的文件转移至该目录下的target目录下 须保证target目录不存在@echo off md target\ for /f "d ...
- 【CentOS】LNMP
本文为博主JerryChan所有,如需转载,请联系博主747618706@qq.com,并附上博客链接/////////////////目录//////////////////一.LNMP的安装 1. ...
- Tween Animation----Translate位置移动动画
布局: 在res/下新建一个anim文件夹用来保存动画xml文件 新建一个translate文件 代码: <?xml version="1.0" encoding=" ...
- xml_TO_object
一般对于开发人员拿到的xml文件都是配置文件,所以对于我们来说,最主要要做的事情是将xml的内容封装成对象. 下面展示代码 package javaDom4j; import java.util.Ar ...