Number formatting output

Source: Internet
Author: User

Int a = 12345678;
// Output in the sring format
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;

// Output in special string format
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;
Custom mode output:

// "0" Description: placeholder. If possible, fill the position
Label1.Text = string. Format ("{0: 00}", a); // 001234
Label2.Text = string. Format ("{0: 00}", B); // 004321

// "#" Description: placeholder. If possible, fill the position.
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

// "." 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;

// "," Description: Numeric grouping, 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

// "%" 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%

// "Abc" Description: displays the text in single quotes.
Label1.Text = string. Format ("{0: 'text' 0}", a); // text 12345678
Label2.Text = string. Format ("{0: Text 0}", B); // text 87654321

// "" Description: the character that is followed by one to be printed, also used for the 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

// "@" Description: The character followed by the word 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

JavaScript processing string
Although JavaScript is useful, processing strings is one of the most popular. The following describes how to use JavaScript to operate strings. In JavaScript, String is an object. String objects are not stored as character arrays, so we must use built-in functions to manipulate their values. These built-in functions provide different methods to access the content of string variables. Let's take a closer look at these functions.
All-encompassing
The value of the operation string is common for developers. There are many methods to operate on a string, such as extracting part of the content from a string or determining whether a string contains a specific character. The following JavaScript Functions provide developers with all the functions they need:
? Concat ()-combines two or more characters to return a new string.
? IndexOf ()-returns the index at the first part of a substring in a string. If no match exists,-1 is returned.
? CharAT ()-returns the character at the specified position.
? LastIndexOf ()-returns the index of the last occurrence of a substring in the string. If no match exists,-1 is returned.
? Match ()-check whether a string matches a regular expression.
? Substring ()-returns a substring of a string. The input parameters are the start position and end position.
? Replace ()-used to find a string that matches a regular expression, and then use a new string to replace the matched string.
? Search ()-execute a regular expression to match the search results. If the search is successful, the matching index value in the string is returned. Otherwise,-1 is returned.
? Slice ()-extract part of the string and return a new string.
? Split ()-divides a string into substrings to form a string array.
? Length ()-returns the length of a string. The so-called length of a string refers to the number of characters it contains.
? ToLowerCase ()-converts the entire string into lowercase letters.
? ToUpperCase ()-converts the entire string into uppercase letters.
Note: The concat, match, replace, and search functions are added to JavaScript 1.2. All other functions are provided in JavaScript 1.0.

Next let's take a look at how to use these functions in JavaScript. The following code uses all the functions mentioned above:
Function manipulateString (passedString1, passedString2 ){
Var concatString;
// The string passed to concat is added to the end of the first string
ConcatString = passedString1.concat (passedString2 );
Alert (concatString );
// The following if statement will be true since first word is Tony
If (concatString. charAt (3) = "y "){
Alert ("Character found! ");
}
// The last position of the letter n is 10
Alert ("The last index of n is:" + concatString. lastIndexOf ("n "));
// A regular expression is used to locate and replace the substring
Var newString = concatString. replace (/Tony/gi, "General ");
// The following yields Please salute General Patton
Alert ("Please salute" + newString );
// The match function returns an array containing all matches found
MatchArray = concatString. match (/Tony/gi );
For (var I = 0; I <matchArray. length; I ++ ){
Alert ("Match found:" + matchArray [I]);
}
// Determine if the regular expression is found, a-1 indicates no
If (newString. search (/Tony/) =-1 ){
Alert ("String not found ");
} Else {
Alert ("String found .");
}
// Extract a portion of the string and store it in a new variable
Var sliceString = newString. slice (newString. indexOf ("l") + 2, newString. length );
Alert (sliceString );
// The split function creates a new array containing each value separated by a space
StringArray = concatString. split ("");
For (var I = 0; I <stringArray. length; I ++ ){
Alert (stringArray [I];
}
Alert (newString. toUpperCase ());
Alert (newString. toLowerCase ());
}
The following is the result of executing the above Code:
Tony Patton
Character Found!
The last index of n is: 10
Match found: Tony
Please salute General Patton
String not found
Patton
Tony
Patton
GENERAL PATTON
General patton
The sample code uses all the mentioned functions.
Special characters
In addition to these functions, there are many special characters that can be used to express key effects. These special characters include:
? "T-hop key
? "B-return/delete
? "R-press ENTER
? "N-line feed
? "F-form feed
The most common use of special characters is to format the output. For example, you may need to insert a line feed in the output to correctly display a value. In addition, you also need to press Enter when changing the line. On Some platforms, "" n "is enough to generate a line feed, while" r "n" is required to display a line feed correctly on some machines ". The following example shows the special characters displayed in a multiline window:
Var output = null;
Output = "Special Characters ";
Output + = "" n ";
Output + = "==================== ";
Output + = "" n ";
Output + = "" t-tab ";
Output + = "" n ";
Output + = "" B-backspace/delete ";
Output + = "" n ";
Output + = "" r-carriage return ";
Output + = "" n ";
Output + = "" n-newline ";
Output + = "" n ";
Output + = "" f-form feed ";
Output + = "" n ";
Alert (output );
In the preceding example, the plus sign is used to connect strings without the concat function. The reason is simple. For the concat function, each operation requires a new variable. On the contrary, the method we use here simply expands the original value without the new variable. In addition, the special characters are correctly displayed using the Escape Character in the example. The system regards a backslash as a signal and considers it to be followed by a special character, but the operation is offset by two backslashes. Each character in the output is displayed on the new line through the special newline character.
Add to toolbox
Special characters and functions can be combined with other JavaScript techniques to solve many problems. One of the cases is used for JavaScript client form verification. The method proposed in this article can be simply used for form verification.
The following code is called when a form is submitted. The form to be submitted contains three fields: name, address, and zip code. To achieve this, we only verify that each domain cannot be empty and the zip code must be a number. The following JavaScript code completes this function:
Function validation (){
Var doc = document. forms [0];
Var msg = "";
If (doc. Name. value = ""){
Msg + = "-Name is missing" n ";
}
If (doc. Address. value = ""){
Msg + = "-Address is missing" n ";
}
If (doc. ZipCode. value = ""){
Msg + = "-Zip code is missing" n ";
}
Var zip = new String (doc. ZipCode. value );
If (zip. search (/^ [0-9] [0-9] [0-9] [0-9] [0-9] $/) =-1 ){
Msg + = "-Enter valid Zip code ";
}
If (msg = ""){
Doc. submit;
} Else {
Msg = "Please correct the following validation errors and re-submit:" n "+ msg;
Alert (msg );
}
}
When a user submits a form, this function will be called. Function calls are implemented in the onSubmit event of an HTML button.
<Input type = "button" type = "submit" value = "submit" onClick = "validation ()">
Verify the function to check whether each field is empty. If a null value is found, an error message is added after the message variable msg is verified. In addition, a regular expression is used to verify the format of the zip code domain. Here, we only accept the five-digit U.S. Postal code. If any error is found (that is, the msg variable is not empty), the program will display an error message; otherwise, the program will submit the form.

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.