国产探花免费观看_亚洲丰满少妇自慰呻吟_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"
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 进贤县| 双柏县| 寿阳县| 长岭县| 唐山市| 保亭| 宣城市| 白银市| 镇雄县| 托克逊县| 兰州市| 依安县| 城口县| 集安市| 察哈| 都兰县| 舒兰市| 邢台县| 边坝县| 双牌县| 庐江县| 高碑店市| 登封市| 青海省| 南丹县| 客服| 盘山县| 普陀区| 新巴尔虎右旗| 绍兴县| 乐山市| 临夏市| 桃园县| 商洛市| 新乡县| 花莲县| 友谊县| 新沂市| 赤壁市| 交城县| 广平县|