Numerical format transformation inside. Net

Source: Internet
Author: User
Tags numeric string format tostring
Every. NET Framework base data type (Int32, Int64, single, Double, and, in) contains a Format method and a ToString meth OD This return a string-formatted representation of the original value. The Format method provides several formatting options and the ability to dynamically change output based on the current CU Lture. The ToString method provides only one formatting option and isn't influenced by the current culture.

The format method is useful if you are want to convert one of the standard. NET Framework data types to some other format. For example, if your have an integer value of this you want to represent to the user as a currency value, you could EAS ily use the Format method to produce a string of "$100.00". Note that the original value contained in the data type isn't converted, but a new string is created that represents the Original value. This new string cannot being used for calculation until it are converted back to a. NET base data type. The original value, however, can continue to is calculated at anytime.

The Format method takes the following forms, where XXX is the name of the numeric base data-type:

static XXX format (xxx value, String Format);
static XXX format (xxx value, String Format,
NumberFormatInfo info);
Static XXX format (String format,
Iserviceobjectprovider s);
The Format method takes a combination of three parameters:a value, a string Format specifier, and a iserviceobjectprovid Er interface.

The Iserviceobjectprovider flag is used to the culture. IT specifies characters to use for decimal and thousand separators and spelling of placement. Currency. When set to null (in Visual Basic nothing), it would use the characters specified by the current culture.

Two format strings are accepted by the format Method:standard numeric format strings and picture numeric format strings. Standard format strings are built-in options that address the most common formatting requirements, while picture strings a Re constructed by developers to address additional formatting requirements.

In following examples the Format method displays the value of as a currency-formatted string in the console ' s output w Indow:

[C #]
int MyInt = 100;
Myint.format ("C", null);
Returns "$100.00"
In this example, the Format method takes two parameters:the format string and Iserviceobjectprovider. The format string specifies the "return value" of the Format method. In this case, the string has a value of "C", which specifies which the Format method would return a currency representation of the original value. You can safely set the "Iserviceobjectprovider flag to NULL", specifying that the default current culture would be Used. Note This Format method returns a string of data type, not a numeric data type.

The following example is similar to the previous example, and except that the value isn't retrieved from a previously declare d int variable:

[C #]
Int. Format (M, "C");
Returns "$100.00"
In this example, the ' value to ' is passed as the ' the ' parameter and the format string is passed as the second. The. NET Framework allows you to format any value without actually declaring and initializing it as a variable if The above syntax.

The ToString method quickly converts a. NET Framework base type value into a string. It requires no arguments. The following example converts an integer value of a string value:

int MyInt = 42;
String MyString = Myint.tostring ();
Mystring would have a string value of "42"


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.