.net里面的数值格式变换
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"
相关文章:
- · .NET之ASP WebApplication快速入门(5)(转载)
- · .NET之ASP Web Application快速入门(1)(转载)
- · .NET之ASP Web Application快速入门(2) (转载)
- · ASP.NET中的错误处理支持
- · ASP.NET中的代码分离
- · 在ASP.NET中使用AdRotator控件(转)
- · 在ASP.NET中动态生成图形(转)
- · 用ASP.NET加密口令(转)
- · ASP.NET中的错误处理支持(转)
- · ASP.NET中发送Email完整实例(转)
- · ASP.NET中的注释符号
- · 老外编的程序(八):在CSharp里面使用Http Get方法
- · 显示当前浏览器头信息(HEADER)
- · 轮换广告
- · 一个dnslookuo例子。。。
- · beta2的web.config配置
- · 如何在web.config中建立公用的的数据库连接
- · asp.net key considerations(三)
- · asp.net key considerations(二)
- · WebPoll in C#[Vs.net Bate2 等级:中 高]
- · Form code generator V1.1 by Steve Schofield[bate2 等级:中级](转载:aspfree)
- · asp.net key considerations(一)从前用惯了asp的朋友看看这个吧,大家常问的如Request等问题解答得很清楚
- · 使用DataList进行3层编历
- · part1: ShowIssueCat.aspx.cs
- · part2: ShowIssueCat.aspx
- · DataNavigateUrlFormatString的使用方法
- · ASP.NET XML/XSL Transforms(转载www.aspalliance.com)
- · Hiding/Manipulating Databound Items(转载www.aspalliance.com)
- · 无刷新的聊天室的制作兼谈组件制作和ClientSide Script(一)
- · 关于异常捕获
- · 无刷新的聊天室的制作兼谈组件制作和ClientSide Script(二)
- · 转贴:用ASP.NET结合XML制作广告管理程序
- · 转贴:用ASP.NET结合XML制作广告管理程序(2)
- · ASP.NET: Dynamically set Text and Value fields for a DropDownList
- · asp.net优化(二)
- · DotNet中定制自己的表格
- · 回答讨饭猫之asp.net优化(一)
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--8
