当前位置:编程学习 > C#/ASP.NET >>

csharp Format CultureInfo

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Text;
using System.Threading;
 
 
namespace sitemapdemo
{
 
    public partial class FormatStringForm : System.Web.UI.Page
    {
 
 
        /// <summary>
        /// http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.71).aspx
        ///Geovin Du 塗聚文 20120510 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            // Format a negative integer or floating-point number in various ways.
            int integralVal = -12345;
            double floatingVal = -1234.567d;
 
            string msgCurrency = "(C) Currency: . . . . . . ";
            string msgDecimal = "(D) Decimal:. . . . . . . ";
            string msgScientific = "(E) Scientific: . . . . . ";
            string msgFixedPoint = "(F) Fixed point:. . . . . ";
            string msgGeneral = "(G) General (default):. . ";
            string msgNumber = "(N) Number: . . . . . . . ";
            string msgPercent = "(P) Percent:. . . . . . . ";
            string msgRoundTrip = "(R) Round-trip: . . . . . ";
            string msgHexadecimal = "(X) Hexadecimal:. . . . . ";
 
            string msg1 = "Use ToString(String) and the current thread culture.\n";
            string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
            string msgCulture = "Culture:";
            string msgIntegralVal = "Integral value:";
            string msgFloatingVal = "Floating-point value:";
 
            CultureInfo ci;
 
            Response.Write("Standard Numeric Format Specifiers:\n");
            // Display the values.
            Response.Write(msg1+"<br/>");
            //Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH", false);
            // Display the thread current culture, which is used to format the values.
            ci = Thread.CurrentThread.CurrentCulture;
            string s=String.Format("{0,-26}{1}", msgCulture, ci.DisplayName);
            Response.Write(s + "<br/>");
            Response.Write(msgCurrency + floatingVal.ToString("C") + "<br/>");
            // Display the integral and floating-point values.
            s = String.Format("{0,-26}{1}", msgIntegralVal, integralVal);
            Response.Write(s + "<br/>");
            s = String.Format("{0,-26}{1}", msgFloatingVal, floatingVal);
            Response.Write(s + "<br/>");
            //Response.Write();
            s = string.Format("{0:d}", System.DateTime.Now); ///结果为:2009-3-20 (月份位置不是03)
            Response.Write(s + "<br/>");
            s = string.Format("{0:D}", System.DateTime.Now); //结果为:2009年3月20日
            Response.Write(s + "<br/>");
            s = string.Format("{0:f}", System.DateTime.Now); //结果为:2009年3月20日 15:37
            Response.Write(s + "<br/>");
            s = string.Format("{0:F}", System.DateTime.Now); //结果为:2009年3月20日 15:37:52
            Response.Write(s + "<br/>");
            s = string.Format("{0:g}", System.DateTime.Now); // 结果为:2009-3-20 15:38
            Response.Write(s + "<br/>");
            s = string.Format("{0:G}", System.DateTime.Now); //结果为:2009-3-20 15:39:27
            Response.Write(s + "<br/>");
            s = string.Format("{0:m}", System.DateTime.Now); //结果为:3月20日
            Response.Write(s + "<br/>");
            s = string.Format("{0:t}", System.DateTime.Now); //结果为:15:41
            Response.Write(s + "<br/>");
            s = string.Format("

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,