C # number formatting output is something we often need to deal with in programming. Here we will introduce some examples of C # number formatting output, which will facilitate your selection and comparison, which method is suitable for your project.
Int A = 12345678;
C # format the number to be formatted as sring output
Label1.text = string. Format ("asdfadsf {0} adsfasdf", );
Label2.text = "asdfadsf" + A. tostring () + "adsfasdf ";
Label1.text = string. Format ("asdfadsf {0: c} adsfasdf", a); // asdfadsf ¥0. 1,234.00 adsfasdf
Label2.text = "asdfadsf" + A. tostring ("C") + "adsfasdf"; // asdfadsf ¥0. 1,234.00 adsfasdf
Double B = 1234.12543;
Int A = 12345678;
C # The format of number formatting is special string style output
Label1.text = string. Format ("asdfadsf {0: c} adsfasdf", B); // asdfadsf ¥0. 1,234.13 adsfasdf
Label2.text = "asdfadsf" + B. tostring ("C") + "adsfasdf"; // asdfadsf ¥0. 1,234.13 adsfasdf
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); // in decimal format -- 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); // set the number of points -- 12345678.00
Label2.text = B. tostring ("F"); // set the number of points -- 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 -- cannot be converted with decimals, error
Label1.text = string. Format ("{0: g}", a); // use the most compact mode -- 12345678
Label2.text = B. tostring ("G"); // use the most compact version -- 1234.12543
Label1.text = string. Format ("{0: R}", a); // transfer without loss of precision -- integers are not allowed and an error is returned.
Label2.text = B. tostring ("R"); // transfer without loss of precision -- 1234.12543
Double B = 4321.12543;
Int A = 1234;
C # custom output of number formatting:
C # digit formatting-0 Description: placeholder. If possible, fill a digit.
Label1.text = string. Format ("{0: 00}", a); // 001234
Label2.text = string. Format ("{0: 00}", B); // 004321
C # digit formatting: "#" Description: placeholder. If possible, fill in a digit.
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 # digit formatting "." Description: decimal point
Label1.text = string. Format ("{0: 000. 000}", a); // 1234.000
Label2.text = string. Format ("{0. 000}", B); // 4321.125
Double B = 87654321.12543;
Int A = 12345678;
C # digit formatting "," Description: Number Group, also used for the Multiplier
Label1.text = string. Format ("{0, 00}", a); // 12,345,678
Label2.text = string. Format ("{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: The format is percent
Label1.text = string. Format ("{0%}", a); // 1234567800%
Label2.text = string. Format ("{0: # %}", B); // 8765432113%
Label1.text = string. Format ("{0. 00%}", a); // 1234567800.00%
Label2.text = string. Format ("{0: #. 00%}", B); // 8765432112.54%
C # digit formatting-"ABC" Description: displays text in single quotes
Label1.text = string. Format ("{0: 'text' 0}", a); // text 12345678
Label2.text = string. Format ("{0: Text 0}", B); // text 87654321
C # "\" Description of number formatting: Followed by a character of 1 to print, also used for escape 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 # "@" Description of number formatting: followed by the characters to be printed,
Label1.text = string. Format (@ "Hello! "); //" Hello! "Print" requires two pairs.
Label2.text = string. Format (@ "\ c \ books \ new \ We. asp"); // \ c \ books \ new \ We. asp
C # The basic information about digit formatting is introduced here. I hope you can understand and learn how to format C # numbers.