Formatting numeric strings with C # variables

Source: Internet
Author: User

Syntax for format specifiers

Justification specifier

Positive numbers indicate right alignment, and negative numbers indicate left alignment. If the number of characters you want to represent is less than the number specified in the alignment specifier, a space is filled in, and if more, it is ignored.

1Staticvoid Main (String[] args) 2  {3 var temp = 1000; "|{ 0,10}|  "5 Console.WriteLine ( | {0,-10}|  "6 Console.WriteLine ( | {0,3}|  "7}             

Format field

1var temp =123.456789;2 Console.WriteLine ("{0:F3}", temp);//Fixed point, number of decimal digits reserved3 Console.WriteLine ("{0:C}", temp);//Represents currency, depending on the locale of the PC4 Console.WriteLine ("{0:D10}",123);//Decimal number5 Console.WriteLine ("{0:G4}", temp);//Keep "number Length" according to the specifier, and the last one rounded6 Console.WriteLine ("{0:X}",123);//Turn 16 binary (case sensitive)7 Console.WriteLine ("{0:N4}1234567,890123"; Span style= "color: #008000;" >// 8 Console.WriteLine ( "{0:p}", temp); // 9 Console.WriteLine ( "{0:r} //10 Console.WriteLine (  "{0:e3}// scientific notation (case-sensitive)           

Use of variables

A variable is a name that represents the data stored in memory when the program executes, and is used for program Access data.

Variable declaration

Before you can use a variable, you must declare it: give the variable a name and associate a type, and let the compiler allocate a chunk of memory for it. (A long piece of crap)

  String name; // declares a variable that defines the name as a type string 

You can also put multiple variable declarations in a single statement, but must be of the same type, with a comma interval between them.

  

Types of variables

Local variables hold temporary data at the method scope, value types are stored in the stack, references to reference types are stored in the stack, and data is stored in the heap.

A field holds data related to a type or type instance, is a member of a type, so the field is stored in the heap, whether it is a value type or a reference class type.

parameter is used for one method to pass data to another method temporary variable

A member of an ordered set of data of the same type of an array element

Variables that can be initialized automatically
1ClassProgram2{3Staticint number;//Class field4Staticvoid Main (String[] args)5{6Console.WriteLine (number);7byte[] Arrs =Newbyte[4]; 8 Console.WriteLine (Arrs[1  9 anystruct any= new Anystruct ();  Ten Console.WriteLine (Any.field); // structure field 11 } 12 }13 14 struct anystruct {public double field; 16}             

Thinking

Does the following code have an exception?

Formatting numeric strings with C # variables

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.