C# ToString格式控制符
|
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++流格式控制符的使用【来自网络】
注意添加<iomanip>头文件. 使用控制符控制输出格式 控制符 作用 dec 设置整数的基数为10 hex 设置整数的基数为16 oct 设置整数的基数为8 setbase(n) 设置 ...
- C语言的格式控制符
1. 格式控制符 格式输出printf 作用是向终端输出若干个类型任意的数据. 格式:printf (格式控制符,输出列表) 1) 格式控制符 l % ...
- C#.ToString()格式大全
原文:C#.ToString()格式大全 C#.ToString()格式大全 stringstr1=string.Format("{0:N1}",56789); ...
- Swift学习——格式控制符和元组流程控制(二)
Swift中的格式控制符和元祖 (1)首先介绍一下元祖,元祖是关系型数据库中 比如学生表中的姓名,年龄,电话等 定义例如以下 var studentinfo = ("jhon", ...
- scanf格式控制符之%[]的应用
考虑只读入小写字母的字符串,这个问题要如何用scanf解决呢? 这就用到了%[] 这个格式控制符,它支持a-z这样的格式控制 char s[111]; scanf("%[a-z]" ...
- 文件重定向,getline()获取一样,屏幕输出流,格式控制符dec,oct,hex,精度控制setprecision(int num),设置填充,cout.width和file(字符),进制输入
1.在window下的命令重定向输出到文件里 2.将内容输入到某个文件里的方式:命令<1.txt (使用1.txt中的命令) 3.读取文件里的名,然后将命令读取最后输出到文件里.命令< ...
- C#中ToString()格式详解
以下内容均摘自博客园,仅供资料查询. ToString格式化 在很多对象显示为字符串的时候都会使用到ToString中的格式化,由于以前没怎么注意到这个问题,想总结一下各个基础结构对象的格式化,以便后 ...
- C#中ToString格式大全
更多资源:http://denghejun.github.io C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5&q ...
- C# Tostring格式
开发中经常用到格式化,不管是时间.货币.数字都可以随心所欲.也许你用的是{0:C}方式,也许你用String.Format方式,也许你用.ToString("n"),都是格式化的方 ...
随机推荐
- Atitit.软件GUIbutton与仪表盘--db数据库区--导入mysql sql错误的解决之道
Atitit.软件GUIbutton与仪表盘--db数据库区--导入mysql sql错误的解决之道 Keyword::截取文本文件后部分 查看提示max_allowed_packet限制 Targe ...
- Emojicon - 为你提供emoji表情的整套方案
Github : https://github.com/rockerhieu/emojicon 事实上国内的<表情大全>也有提供类似整套表情解决方式,并且还支持gif表情:http://w ...
- 通过MyEclipse工具直接操作数据库,执行sql语句,方便快捷
原文:通过MyEclipse工具直接操作数据库,执行sql语句,方便快捷 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEcl ...
- MEF框架在Silverlight中应用(15)
原文:MEF框架在Silverlight中应用(15) MEF框架在Silverlight中应用 代码下载 MEF框架是微软提供的一个插件框架.应用概括为一句话:输入,输出,组合.下面是具体在Silv ...
- <C++ 实现设计模式> 观察者模式
观察者模式,又称公布--订阅,mvc模式等. 通俗点讲,比方股票来说,非常多人关注一支股票,派一个人去观察股票的情况,一有变化(观察),就通知全部的预定这个消息的人. 而我们常见的mvc模式,v是指v ...
- 推荐几个靠谱的VPN
最近开发scala程序使用sbt构建工程时,下载很慢,有些依赖只能通过VPN才能下载下来.在网上搜索了一大堆提供VPN服务的.有一大部分不提供试用,而且还必须得按年购买,看起来是像骗子. 在这里推荐几 ...
- JS模块与命名空间的介绍二
区别一:
- Python 2.7.3 Time与DateTime格式化
import time import datetime class TimeX: '''时间工具,目前用于格式化时间''' @staticmethod def GetLocalTimeString_T ...
- 小公司免费的ERP软件
http://www.2bizbox.cn/ https://www.odoo.com/
- 【SSRS】入门篇(一) -- 创建SSRS项目
原文:[SSRS]入门篇(一) -- 创建SSRS项目 在本篇中,您将学习如何在 SQL Server Data Tools (SSDT) 中创建报表服务器项目. 报表服务器项目用于创建在报表服务器中 ...