C#语言学习代码示例

一、关于字符串格式:保留数位

namespace BasicGrammarStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(string.Format("{0:F3}", 13.47483327438));  // 13.475
            Console.WriteLine(string.Format("{0:N3}", 13.47483327438));  // 13.475
            Console.WriteLine(string.Format("{0:F}", 13.47483327438));  // 13.47
            Console.WriteLine(13.47483327438.ToString("0.###"));  // 13.475
            Console.WriteLine(Math.Round(13.47483327438, 3));  // 13.475
            Console.WriteLine(Math.Round(13.47483327438, MidpointRounding.AwayFromZero));  // 13
            Console.ReadKey();
        }
    }
}

输出:
13.475
13.475
13.47
13.475
13.475
13

 二、关于进制转换:十六进制数加上一个十进制数然后以十六进制返回结果

{
    class Program
    {
        // 十六进制加十进制
        static string hexAddDec(string hex, int addNum)
        {
            int dec1 = Convert.ToInt32(hex, 16);
            Console.WriteLine(dec1);  // 26
            return string.Format("{0:X}", dec1 + addNum);
        }
        static void Main(string[] args)
        {
            Console.WriteLine(hexAddDec("0x1a", 23));  // 49-->31
            Console.ReadKey();
        }
    }
}

 三、关于关键字 implicit and explicit。

不同类型之间的随意转换,分为隐式和显示两种,隐式就是直接赋值,不需要其他操作,而显式需要多一个关键字,看起来更清晰明白。

class ComplexNumber
    {
        public static void CMTest()
        {
            Exponential expo = new Exponential(3, 4);
            Console.WriteLine(expo);
            Trigonometric tri = expo;
            Console.WriteLine(tri);

            Trigonometric tri1 = new Trigonometric(4, 5);
            Console.WriteLine(tri1);
            // Exponential expo1 = tri1;  // 报错,缺少显式转换
            Exponential expo1 = (Exponential)tri1;
            Console.WriteLine(expo1);
            Console.ReadLine();
        }
        public struct Trigonometric
        {
            public int a;
            public int b;

            public Trigonometric(int a, int b)
            {
                this.a = a;
                this.b = b;
            }
            public static implicit operator Trigonometric(Exponential expo)
            {
                var tri = new Trigonometric();
                tri.a = expo.a;
                tri.b = expo.b;
                return tri;
            }

            public override string ToString()
            {
                return string.Format("{0} cos(x) + {1} i sin(x)",this.a, this.b);
            }
        }
        public struct Exponential
        {
            public int a;
            public int b;
            public Exponential(int x1, int y1)
            {
                this.a = x1;
                this.b = y1;
            }
            public static explicit operator Exponential(Trigonometric tri)
            {
                var expo = new Exponential();
                expo.a = tri.a;
                expo.b = tri.b;
                return expo;
            }
            public override string ToString()
            {
                return string.Format("exp({0}) × exp(i{1}x)", this.a, this.b);
            }
        }
    }

static void Main(string[] args)
{
    ComplexNumber.CMTest();
}

end。

 

posted @ 2023-08-16 10:09  倦鸟已归时  阅读(44)  评论(0编辑  收藏  举报