首页 > 编程 > .NET > 正文

.net里面的数值格式变换

2024-07-10 12:58:16
字体:
来源:转载
供稿:网友

every .net framework base data type (int32, int64, single, double, and so on) contains a format method and a tostring method that 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 culture. the tostring method provides only one formatting option and is not influenced by the current culture.

the format method is useful if you want to convert one of the standard .net framework data types to some other format. for example, if you have an integer value of 100 that you want to represent to the user as a currency value, you could easily use the format method to produce a string of "$100.00". note that the original value contained in the data type is not converted, but a new string is created that represents the original value. this new string cannot be used for calculation until it is converted back to a .net base data type. the original value, however, can continue to be 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 an iserviceobjectprovider interface.

the iserviceobjectprovider flag is used to change the culture. it specifies characters to use for decimal and thousand separators and the spelling and placement of currency symbols. when set to null (in visual basic nothing), it will 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 are constructed by developers to address additional formatting requirements.

in following examples the format method displays the value of 100 as a currency-formatted string in the console's output window:

[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 that the format method will return a currency representation of the original value. you can safely set the iserviceobjectprovider flag to null (nothing), specifying that the default current culture will be used. note that the format method returns a string data type, not a numeric data type.

the following example is similar to the previous example, except that the value is not retrieved from a previously declared int variable:

[c#]
int.format( 100, "c");
//returns "$100.00"
in this example, the value to be formatted is passed as the first 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 you use the above syntax.

the tostring method quickly converts a .net framework base type value into a string. it requires no arguments and is easy to use. the following example converts an integer value of 42 into a string value:

int myint = 42;
string mystring =  myint.tostring();
//mystring will have a string value of "42"
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表