Use of the String. Format () method in C,

Source: Internet
Author: User
Tags format definition

Use of the String. Format () method in C,

 

I. Definition
String. Format is to replace each Format item in the specified String type data with the text equivalent item of the corresponding object value.
For example:

(1)
string p1 = "Jackie";
string p2 = "Aillo";
Response.Write(String.Format("Hello {0}, I'm {1}", p1, p2));
(2)
Response.Write(String.Format("Hello {0}, I'm {1}", "Jackie", "Aillo"));

The two have the same effect. Both Replace the values of the last two items with the {0} and {1} of the first item respectively }.
The output result is: Hello Jackie, I'm Aillo

Ii. Multi-Format definition of String. Format:
The so-called multi-format refers to the definition of 1 ~ in a format item ~ Three format parameters. Each format parameter is separated by a semicolon. The value corresponding to the format items with two and three format parameters must be of the numerical type to determine whether it is negative, positive, or zero.
With 1 format parameter:
// Output in scientific notation format
Double p1 = 1000000;
Response. Write (String. Format ("{0: E2}", p1 ));
With two format parameters:
/* If the value corresponding to the format item is not negative, select the first format. If the value is negative, select the second format */
Double p1 = 10000;
Double p2 =-2420.50;
Response. Write (String. Format ("{0 :#,### 0.00 ;#,## 0.000 ;}< BR>", p1 ));
Response. Write (String. Format ("{0 :#,### 0.00 ;#,## 0.000 ;}", p2 ));
With three format parameters:
/* Select the first format when the value corresponding to the format item is positive;
The negative number is in the second format;
If the value is equal to zero, the third format */
1 double p1 = 10000;
Double p2 =-2420.50;
Double p3 = 0.00;
Response. write (String. format ("{0 :#,### 0.00 ;#,### 0.000 ;#,### 0.0000 }< BR>", p1 ));
Response. write (String. format ("{0 :#,### 0.00 ;#,### 0.000 ;#,### 0.0000 }< BR>", p3 ));
Response. Write (String. Format ("{0 :#,### 0.00 ;#,### 0.000 ;#,### 0.0000}", p2 ));
Supplement:
N3 and f3 in {0: N2} indicate the type of the formatted data and the number of decimal places. For example, N2 indicates a number with two decimal places;
Similar to this:
N or n indicates a number.
F or f indicates a fixed point
E or e Indicates scientific notation
D or d indicates the decimal number.
X or x indicates hexadecimal
G or g indicates regular
C or c indicates currency
This article
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.