C # description of tostring format

Source: Internet
Author: User

C

 

 

Currency

 

 

2.5.tostring ("C ")

 

 

¥2.50

 

 

D

 

 

Decimal number

 

 

25. tostring ("D5 ")

 

 

00025

 

 

E

 

 

Scientific type

 

 

25000. tostring ("e ")

 

 

2.500000e + 005

 

 

F

 

 

Fixed Point

 

 

25. tostring ("F2 ")

 

 

25.00

 

 

G

 

 

General

 

 

2.5.tostring ("G ")

 

 

2.5

 

 

N

 

 

Number

 

 

2500000. tostring ("N ")

 

 

2,500,000.00

 

 

X

 

 

Hexadecimal

 

 

255. tostring ("X ")

 

 

FF

 

Formatcode is an optional format code string. (For details, search for "Format String)

The format must be separated from other characters with "{" and. If braces are also used in the format, you can use two consecutive braces to indicate a braces, namely, "{" or "}".

Examples of common formats:

(1) int I = 12345;

This. textbox1.text = I. tostring ();

// Result 12345 (this indicates the current object or the instance of the current class)

This. textbox2.text = I. tostring ("D8 ");

// Result 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"); // result 12345.68

This. textbox2.text = I. tostring ("F6 ");

// Result 12345.678900

(4) Double I = 12345.6789;

This. textbox1.text = I. tostring ("N"); // result 12,345.68

This. textbox2.text = I. tostring ("N4"); // result 12,345.6789

(5) Double I = 0.126;

String S = string. Format ("the value is {0: p}", I );

This. textbox1.text = I. tostring ("p"); // result 12.6%

This. textbox2.text = s; // The value is 12.6%

(6) datetime dt = new datetime (2007,5, 25 );

This. textbox1.text = DT. tostring ("YY. M. d ");

// Result 03.5.25

This. textbox2.text = DT. tostring ("yyyy-mmonth ");

// Result: February 1, May 2003

Convert. todatetime ("2005/12/22 22:22:22"). tostring ("yyyy/mm/dd hh: mm: SS ")
"22:22:22"

(7) int I = 123;

Double J = 123.45;

String S = string. Format ("I: {0,-7}, J: {1, 7}", I, j );

//-7 indicates the left alignment, which occupies 7 digits

This. textbox1.text = s;

// Result I: 123, J: 123.45

 

Byte. tostring method (string, iformatprovider) C # example

 

The following code example usesTostringMethods for reload formattingByteValue.

// 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 }",
"No Format String:", smallvalue. tostring (),
Largevalue. tostring ());
Console. writeline ("{0,-20 }",
"'X2 'format string:", smallvalue. tostring ("X2 "),
Largevalue. tostring ("X2 "));

// Get the numberformatinfo object from
// 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 }",
"No Format String:", smallvalue. tostring (numinfo ),
Largevalue. tostring (numinfo ));
Console. writeline ("{0,-20 }",
"'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
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
*/

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.