国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .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"
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普定县| 耒阳市| 津市市| 长子县| 泾川县| 安西县| 彰化县| 沈阳市| 唐山市| 那坡县| 神木县| 肇东市| 张家港市| 若羌县| 土默特左旗| 吉木萨尔县| 洞口县| 郎溪县| 东山县| 瑞昌市| 屏南县| 封开县| 武邑县| 定结县| 长丰县| 大兴区| 崇左市| 乐东| 永胜县| 鄂尔多斯市| 东海县| 冷水江市| 日土县| 云南省| 潮州市| 汨罗市| 望谟县| 麻江县| 九龙城区| 嘉禾县| 房产|