C # formatted output

Source: Internet
Author: User
Tags compact

int a = 12345678
//format 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;
//format 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);/value--12,345,678.00
Label2.Text = b.tostring ("n");/value--1,234.13
Label1.Text = string. Format ("{0:x}", a);/hexadecimal--bc614e
Label2.Text = b.tostring ("x");//16--with decimal numbers 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 without loss of precision-integer is not allowed, error
Label2.Text = b.tostring ("R"); precision--1234.12543
Double b = 4321.12543;
int a = 1234;

Custom mode output:

//"0" description: Placeholder, if possible, padding bit
Label1.Text = string. Format ("{0:000000}", a);//001234
Label2.Text = string. Format ("{0:000000}", b);//004321
//"#" description: Placeholder, if possible, padding 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
///"." 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;
//", Description: Group of numbers, also 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);//
Label2.Text = string. Format ("{0:0,,}", b);//The
Label1.Text = string. Format ("{0:0,,,}", a);//0
Label2.Text = string. Format ("{0:0,,,}", b);//0
//"%" description: Formatted as percent
Label1.Text = string. Format ("{0:0%}", a);//1234567800%
Label2.Text = string. Format ("{0:#%}", b);//8765432113%
Label1.Text = string. Format ("{0:0.00%}", a);//1234567800.00%
Label2.Text = string. Format ("{0:#.00%}", b);//8765432112.54%
//"ABC" Description: Display text within single quotes
Label1.Text = string. Format (' {0: ' text ' 0} ', a);//text 12345678
Label2.Text = string. Format ("{0: Text 0}", b);//Text 87654321
//"\" description: followed by 1 characters to be printed and also for the transfer character \ n
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
//"@" Description: followed by the character to be printed,
Label1.Text = string. Format (@ "" Hello!) """); Hello To print, you need to enter a two pair to
Label2.Text = string. Format (@ "\c\books\new\we.asp");//\c\books\new\we.asp

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.