• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

.net里面的数值格式变换

发布: 2007-7-01 11:09 | 作者: admin | 来源: | 查看: 20次 | 进入软件测试论坛讨论

领测软件测试网 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"

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网