C # Learning-Illustrated Tutorial (1): Formatting numeric strings

Source: Internet
Author: User
Tags alphabetic character

Learning content: C #; Learning books: Illustrated Tutorials (Chinese fourth edition).

Table of Contents: Chapter II Programming Overview of C #-----> 2.7 outputting text from programs-----> 2.7.5 Formatting numeric strings

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_2018_05_04_am_1520_ Formatting A numeric string {classProgram {Static voidMain (string[] args) {            /** Format numeric String * format specifier syntax consists of 3 fields: index number, alignment specifier, format field * Format: {index, Alignmen T:format} * Index is required to be filled in, indicating which one needs to be formatted; * Alignment is the alignment specifier and is determined by itself whether or not it needs to be filled out; is an integer that is             integers represent right-aligned fields, negative integers represent left-aligned, numbers fill in the length (width), and format is the formatted field that specifies the representation of the number selected by index; Be aware that the colon is followed by a format specifier, which cannot be separated by a space.             * The format specifier is an alphabetic character; the precision specifier is optional or not, and consists of one or two digits. * For details, please see the following example*/            DoubleFirst =1234.56789; DoubleSecond =-12.3456789; /** Standard numeric format specifier*/Console.WriteLine ("format specifier:");
Console.WriteLine ("currency C, C (case insensitive) | {0, 15:c}| {1, -15:c3}|", first, second); /** Format the value as currency, the currency symbol depends on the area of the PC where the program is located automatic * The precision specifier sets the number of decimal places, and if you do not set the precision specifier, it automatically takes 2 decimal places. */

Console.WriteLine ("decimal D, D (case insensitive) | {0,:D}| {1, -15:d 4}|",107, - in); /** Decimal digit string, need to be aware that only with the integer type to use, if the other type will error, prompt format specifier invalid * The precision specifier sets the number of bits of the entire string, if the actual number of digits is less than the given precision description of the number of digits, the left Side complement 0. */

Console.WriteLine ("fixed point F, f (case insensitive) | {0, 15:f}| {1, -15:f3}|", first, second); /** Decimal numeric string with decimal number * The precision specifier sets the number of decimal places, and if you do not set the precision specifier, it automatically takes 2 decimal places. */

Console.WriteLine ("General G, G (case insensitive) | {0, 15:g}| {1, -15:g4}|", first, second); /** In the absence of specifiers, the compact form represented by a value converted to a fixed point or scientific notation * The precision specifier sets the number of bits of the entire string, and if the actual number of digits is less than the given precision specification, no 0 operations are performed; is displayed as-is*/

Console.WriteLine ("hex x, x (case-sensitive) | {0, 15:x5}| {1, -15:x}|",107, - in); /** The hexadecimal digits of the a~f are matched according to the case of the format specifier, x matches the a~f; x matches the a~f * The precision specifier sets the number of bits for the entire string, and if the actual number of digits is less than the given precision description digit, on the left Complement 0. Add: The number of bits in a string is the number of bits that are converted into a 16-based string. */

Console.WriteLine ("number N, n (case insensitive) | {0, 15:n}| {0, -15:n5}|", first, second); /** Similar to the fixed point, but in a group of three numbers with a comma or a space separator, the decimal number to the left, depending on the locale of the PC where the program is located * The precision specifier is set to the number of decimal places, if you do not set the precision specifier then automatically take 2 bit small Number. */

Console.WriteLine ("percent p, p (case insensitive) | {0,:P}| {1, -15:p 6}|", first, second); /** Indicates a percentage, the number is multiplied by 100 * The precision specifier sets the number of decimal places, and if the precision specifier is not set then the 2 decimal places are automatically taken. */
Console.WriteLine ("roundtrip process R, R (case insensitive) | {0, 15:r}| {1, -15:r3}|", first, second); /** If you use the Parse method to convert a string to a number after the output string is guaranteed, the value is the same as the original value. * The accuracy specifier is set and has no effect on the data. */

Console.WriteLine ("Science notation E, E (case-sensitive) | {0, 15:e}| {1, -15:e10}|", first, second); /** Scientific notation with mantissa and exponent. The letter e preceding the exponent is matched according to the case of the specifier. The precision specifier sets the number of decimal digits. */

Console.readkey (); } }}

Learning experience:

I have not yet met these uses in my present job.

C # Learning-Illustrated Tutorial (1): Formatting numeric strings

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.