C#中ToString格式大全

來源:互聯網
上載者:User

標籤:style   color   使用   strong   os   width   

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
*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.