string in C #. Format () and ToString () formatting methods

Source: Internet
Author: User
Tags compact

C # Digital format output is something we often need to deal with in programming, so here are some examples of C # Digital formatted output that will make it easier for you to choose and compare, and what is better for your project.

int a = 12345678;

C # number formatted as sring output

Label1.Text = string. Format ("Asdfadsf{0}adsfasdf", a);

Label2.Text = "ASDFADSF" +a.tostring () + "ADSFASDF";

Label1.Text = string. Format ("Asdfadsf{0:c}adsfasdf", a);//ASDFADSF¥1,234.00ADSFASDF

Label2.Text = "ASDFADSF" +a.tostring ("C") + "ADSFASDF";//asdfadsf¥1,234.00adsfasdf

Double b = 1234.12543;

int a = 12345678;

C # number formatted as a special string-style output

Label1.Text = string. Format ("Asdfadsf{0:c}adsfasdf", b);//asdfadsf¥1,234.13adsfasdf

Label2.Text = "ASDFADSF" +b.tostring ("C") + "ADSFASDF";//asdfadsf¥1,234.13adsfasdf

Label1.Text = string. Format ("{0:c3}", b);//¥1,234.125

Label2.Text = b.tostring ("C3");//¥1,234.125

Label1.Text = string. Format ("{0:d}", a);//Decimal--12345678

Label2.Text = b.tostring ("D");//decimal--same type, conversion error

Label1.Text = string. Format ("{0:e}", a);//Index--1.234568e+007

Label2.Text = b.tostring ("e");//Index--1.234125e+003

Label1.Text = string. Format ("{0:f}", a);//fixed-point number--12345678.00

Label2.Text = B.tostring ("f");//fixed-point number--1234.13

Label1.Text = string. Format ("{0:n}", a);//Numeric--12,345,678.00

Label2.Text = B.tostring ("n");//Numeric--1,234.13

Label1.Text = string. Format ("{0:x}", a);//Hex--bc614e

Label2.Text = b.tostring ("x"),//16--with decimal cannot be converted, error

Label1.Text = string. Format ("{0:g}", a);//Universal is the most compact--12345678

Label2.Text = b.tostring ("G");//Universal is the most compact--1234.12543

Label1.Text = string. Format ("{0:r}", a);//round-off without loss of precision-integer not allowed, error

Label2.Text = b.tostring ("R");//no loss of precision--1234.12543

Double b = 4321.12543;

int a = 1234;

C # Digitally formatted custom mode output:

C # Number Format "0" description: Placeholder, if possible, fill bit

Label1.Text = string. Format ("{0:000000}", a);//001234

Label2.Text = string. Format ("{0:000000}", b);//004321

C # number Formatting "#" description: Placeholder, if possible, fill bit

Label1.Text = string. Format ("{0:#######}", a);//1234

Label2.Text = string. Format ("{0:#######}", b);//4321

Label1.Text = string. Format ("{0: #0 # #}", a);//01234

Label2.Text = string. Format ("{0:0#0000}", b);//004321

C # Digital formatting "." Description: decimal point

Label1.Text = string. Format ("{0:000.000}", a);//1234.000

Label2.Text = string. Format ("{0:000.000}", b);//4321.125

Double b = 87654321.12543;

int a = 12345678;

C # number Formatting "," Description: Number grouping, also used for multiplier

Label1.Text = string. Format ("{0:0,00}", a);//12,345,678

Label2.Text = string. Format ("{0:0,00}", b);//87,654,32

Label1.Text = string. Format ("{0:0,}", a);//12346

Label2.Text = string. Format ("{0:0,}", b);//87654

Label1.Text = string. Format ("{0:0,,}", a);//12

Label2.Text = string. Format ("{0:0,,}", b);//88

Label1.Text = string. Format ("{0:0,,,}", a);//0

Label2.Text = string. Format ("{0:0,,,}", b);//0

C # number Formatting "%" Description: Format is percentage

Label1.Text = string. Format ("{0:0%}", a);//1,234,567,800%

Label2.Text = string. Format ("{0:#%}", b);//8,765,432,113%

Label1.Text = string. Format ("{0:0.00%}", a);//1,234,567,800%

Label2.Text = string. Format ("{0:#.00%}", b);//8,765,432,112,.54%

C # Digital formatting "ABC" description: Display text within single quotation marks

Label1.Text = string. Format ("{0: ' text ' 0}", a);//Text 12345678

Label2.Text = string. Format ("{0: Text 0}", b);//Text 87654321

C # number format "\" Description: followed by 1 characters to print the word, also for the transfer character \ n, etc.

Label1.Text = string. Format ("\" Hello!) \ "");//"Hello!" "

Label2.Text = string. Format ("[url=file://\\c\\books\\new\\we.asp]\\c\\books\\new\\we.asp");//\c\books\new\we.asp

C # Number format "@" description: followed by the character to print the word,

Label1.Text = string. Format (@ "" "Hello! """); Hello "To print" you need to enter two pairs to

Label2.Text = string. Format (@ "\c\books\new\we.asp");//\c\books\new\we.asp

string in C #. Format () and ToString () formatting methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.