Asp.net变量转换与数据类型转换和日期转换

//1.变量区分大小写
           //2.变量默认为null
           //3、局部变量只在花括号内生效。
           //4. bool pd = (m == 2004);

                       
       #region =========================================================== \\ ★ 【常量赋值】

               Response.Write("
----------【常量赋值】 -------------
");
               const int month = 12, week = 11;       //常量pi,π
               const double pi = 3.141592;    //常量pi,π
               const float aaa = 3.15f;
               const string uuu = "我是常量";
               Response.Write(aaa.ToString()+uuu);
                 
       #endregion

       #region =========================================================== \\ ★ 【变量赋值】

           Response.Write("
----------【变量赋值】 -------------
");
           int month1 = 12, week1 = 11;    //常量pi,π
           double pi1 = 3.141592;     //常量pi,π
           float aaa8 = 3.15f;             //Float 必须后面加f,如:float m_dbf=12.23f
           string uuu1 = "我是常量";
           Response.Write("
"+aaa8.ToString() + uuu1);

       #endregion

       #region =========================================================== \\ ★ 【变量转换】


            Response.Write("
----------【变量转换】 -------------
");
          // ------------------------------------- \\ 从该类型 到该类型
           

           // --------------------------------------------- \\ 【日期转换】
                       
            System.DateTime currentTime=new System.DateTime();
            currentTime=System.DateTime.Now;  // 取当前年月日时分秒
            int 年=currentTime.Year;   // 取当前年
            int 月=currentTime.Month;   // 取当前月
            int 日=currentTime.Day;    // 取当前日       
            int 时=currentTime.Hour;   // 取当前时
            int 分=currentTime.Minute;  // 取当前分
            int 秒=currentTime.Second;  // 取当前秒 
            int 毫秒=currentTime.Millisecond;  // 取当前毫秒  

           Response.Write("
" + 年.ToString() + "年" + "
");

            // --------------------------------------------- \\ 【字符型转换32位数字型】

               string sss_0 = "210";
               int sss_8 = Convert.ToInt32(sss_0);   // “整数字符串”转换成“整数”
               Response.Write("
--" + sss_8.ToString() + "--
");


           // --------------------------------------------- \\ 【字符型(小数位)转换数字型】

               string sss_1 = "20.32";
               string sss_2 = "15.67";
               double result = Convert.ToDouble(sss_1) + Convert.ToDouble(sss_2);
               string sss_3 = result.ToString();  //  想让sss_3=sss_1+sss_2(显示结果是35.99)
               string sss_4 = Math.Round(result).ToString();  //  想让sss_3=sss_1+sss_2(显示结果是36(两个数相加四舍五入))
               string sss_5 = ((int)Convert.ToDouble(sss_1) + (int)Convert.ToDouble(sss_2)).ToString();  //  想让sss_3=sss_1+sss_2(显示结果是35(两个数分别取整后相加))

               Response.Write(sss_3 + "
");  // 【显示为:35.99】                         
               Response.Write(sss_4 + "
");  // 【显示为:36】                         
               Response.Write(sss_5 + "
");  // 【显示为:35】                         

           // ---------------------------------------------- \\ 【数值型(小数位)转字符型】

               float aaa1 = 3.15f;
               string aaa2 = (Math.Round((aaa1 * 10)) / 10).ToString();   // 想取出数为:3.2 (也就是保留一位,四舍五入)
               string aaa3 = (Math.Floor((aaa1 * 10)) / 10).ToString();   // 想取出数为:3.1(也就是直接保留一位小数)
               string aaa4 = ((int)aaa1).ToString();   // 想取出数为:3(也就是取整)

               Response.Write(aaa2 + "
");  // 【显示为:3.2】                         
               Response.Write(aaa3 + "
");  // 【显示为:3.1】                         
               Response.Write(aaa4 + "
");  // 【显示为:3】                

       #endregion
           
       #region =========================================================== \\ ★ 【运算符】
           Response.Write("
----------【运算符】 -------------
");
           
       #endregion

       #region =========================================================== \\ ★ 【数组】

           Response.Write("
----------【数组】 -------------
");

               string[] m_name = new string[5] { "王", "安", "李", "和", "马" };
               int[] m_name1 = new int[5] { 1, 54, 12, 5, 2 };

               Response.Write("数组共有【"+m_name.Length.ToString() +"】个数");

           foreach (string u in m_name)
               {
                   Response.Write("显示数组:"+u+"
");
               }

           Array.Reverse(m_name); // ------------------- 颠倒arrStr数组

           foreach (string u in m_name)
           {
               Response.Write("反向显示数组:" + u + "
");

           }

           Array.Sort(m_name);   // -------------------- 排序数组

           foreach (string u in m_name)
           {
               Response.Write("排序数组(字符):" + u + "
");

           }

           Array.Sort(m_name1);   // -------------------- 排序数组

           foreach (int u in m_name1)
           {
               Response.Write("排序数组(数值):" + u.ToString() + "
");

           }

           
       #endregion

       #region =========================================================== \\ ★ 【转义字符】

               Response.Write("
----------【转义字符】 -------------
");
               Response.Write("
【单引号】是" + "\'aaa\'");
               Response.Write("
【双引号】是" + "\"abc\"");
               Response.Write("
【反斜杠】是" + "\\");
               Response.Write("
【空字符】是" + "\0");
               Response.Write("
【警报符】是" + "\a");
               Response.Write("
【退格】是" + "aa\b");
               Response.Write("
【换页】是" + "\f");
               Response.Write("
【换行】是" + "\n\n\n");
               Response.Write("
【回车】是" + "\r");
               Response.Write("
【水平制表符】是" + "\t");
               Response.Write("
【垂直制表符】是" + "\v");
          
           
       #endregion

                          

posted @ 2022-10-28 17:15  Rukh  阅读(106)  评论(0编辑  收藏  举报