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格式大全,布布扣,bubuko.com

时间: 2024-12-18 06:26:27

C#中ToString格式大全的相关文章

C#中的ToString格式大全

// C# 日期格式 DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString();//127756416859912816 dt.ToFileTimeUtc().ToString();//127756704859912816 dt.ToLocalTime().ToString();//2005-11-5 21:21:25 dt.ToLongDateString().ToStri

C#中ToString()格式详解

ToString格式化 在很多对象显示为字符串的时候都会使用到ToString中的格式化,由于以前没怎么注意到这个问题,想总结一下各个基础结构对象的格式化,以便后备之用!!! Int.ToString(format): 格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的有效位数或小数位数,具体如下: 格式说明符 说明 示例 输出 C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString(&q

C#.ToString()格式大全

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 数字 25000

C#时间日期格式大全

C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2016-7-1 14:33:34 如果要换成成201607,07-2016,2016-7-1或更多的该怎么办呢? 我们要用到: DateTime.ToString的方法(String, IFormatProvider) using System; using System.Globalization; String format="D"; DateTime date=DataT

asp.net C# 时间格式大全

asp.net C# 时间格式大全DateTime dt = DateTime.Now;//   Label1.Text = dt.ToString();//2005-11-5 13:21:25//   Label2.Text = dt.ToFileTime().ToString();//127756416859912816//   Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816//   Label4.Text =

Java日期转换SimpleDateFormat格式大全(转)

24小时制时间显示: public class Datetime { public static void main(String args[]){ java.util.Date current=new java.util.Date(); java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String c=sdf.format(current); System.ou

android nfc中Ndef格式的读写

检测到标签后在Activity中的处理流程 1. 在onCreate()中获取NfcAdapter对象: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 2.在onNewIntent()中获取Tag对象或者NdefMessage信息: 获取Tag对象: Tag tag = intent.getParcelableExra(NfcAdapter.EXTRA_TAG); 获取NdefMessage信息: Parcelable[]

ToString()格式转换

Convert.ToByte 方法 (String, Int32) value 中数字的基数,它必须是 2.8.10 或 16. MSDN string.Format的转义符是{} string.Format("Hello, {{Rodolfo}}. Im {0}", "Lanny"); 输出: Hello,{Rodolfo}, Im Lanny ToString("format") C 货币 2.5.ToString("C"

小程序时间格式大全

小程序时间格式大全 util 博客推荐 下面文章方面比较全面,可以看看: JavaScript Date(日期)对象 const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const s