首页 > 编程语言 >C#运输符和转化类型

C#运输符和转化类型

时间:2023-05-05 14:12:47浏览次数:28  
标签:运输 Convert Console C# double 转化 int WriteLine ToInt32

const double PAI= 3.14;//常量
            PAI = 3;

枚举:

    internal class Program
    {
        static void Main(string[] args)
        {
            Gender gender =Gender.Male;
            Console.WriteLine(gender);
            Console.WriteLine((int)gender);//强制转换成int类型
            Console.ReadLine();
        }
    }

    /// <summary>
    /// 枚举
    /// </summary>
    public enum Gender
    {/// <summary>
    /// 男
    /// </summary>
        Male=1,
        /// <summary>
        /// 女
        /// </summary>
        Female=0
    }

强制转换:

值转换:

        /// <summary>
        /// 值转换
        /// </summary>
        public static void mothed()
        {
            double a = 10.44;
            Console.WriteLine(a);
            Console.WriteLine((int)a);//丢失精度
            int b = 16;
            Console.WriteLine((double)b);
            object c = 10.55;
            //Console.WriteLine((int)c);//出错
            //object为引用类型,所以强制转换成int类型,要对应不能有小数,或者先转到对应类型
            Console.WriteLine((int)(double)c);//c是double所以先转double,再int
            Console.ReadLine();
        }

Parse转换:

        /// <summary>
        /// Parse转换,字符串->值类型
        /// </summary>
        public static void mothed1()
        {
            string text = "234.567891230";
            //Console.WriteLine(int.Parse(text));//必须是对应的类型
            Console.WriteLine(double.Parse(text));//double精确到小数点后15位
            Console.WriteLine(float.Parse(text));//float精确到小数点后6位
            Console.ReadLine();
        }

Convert.To转换:(万能,任意都可以转换)

        public static void mothed2()
        {
            string test = "123.590";
            string test1 = "123";
            int a = Convert.ToInt32(test1);//格式要对应
            double b=Convert.ToDouble(test);
            Console.WriteLine(a);
            Console.WriteLine(b);
            double text2 = 13.57;
            int c=Convert.ToInt32(text2);//四舍五入
            Console.WriteLine(c);
            double text3 = 13.49;//舍去
            int d = Convert.ToInt32(text3);
            Console.WriteLine(d);
            //特殊
            double text4 = 17.50;//奇数.50进位
            double text5 = 18.50;//偶数.50舍去
            int e=Convert.ToInt32(text4);
            int f=Convert.ToInt32(text5);
            Console.WriteLine(e);
            Console.WriteLine(f);



            Console.ReadLine();
        }

 

标签:运输,Convert,Console,C#,double,转化,int,WriteLine,ToInt32
From: https://www.cnblogs.com/lin-07/p/17373949.html

相关文章

  • freeswitch-ubuntu安装
    一,软件及环境准备:ubuntu版本18.04:https://releases.ubuntu.com/18.04.6/ubuntu-18.04.6-live-server-amd64.isofreeswitch版本1.10.7:https://files.freeswitch.org/freeswitch-releases/freeswitch-1.10.7.-release.tar.gzspandsp包:https://codeload.github.com/freeswitch/sp......
  • LeetCode 1049. 最后一块石头的重量 II
    思路任何时刻,某个石头的重量永远都是若干石头加减运算的绝对值如a-b+c合并石头都是减法,但仍可能出现+运算符,如a-(b-c)=a-b+c任何一种合并方法,最后一个石头的重量都可以表示成一种代数形式,如a+b-c+d+e+f-g不是所有的代数形式都可以转换为一种合并方法,如a+b+c因此......
  • springcloud小应用
    一、Actuator修改Actuator端点前缀management.endpoints.web.base-path=/manage将原来的mappings端点的请求路径修改为urlMappingsmanagement.endpoints.web.path-mapping.mappings=request_mappings暴露部分端点management.endpoints.web.exposure.include=info,health,be......
  • 清理c盘中的临时文件 释放空间
    临时文件夹里的临时文件非常占内存,找到它们删掉就可以:win+R,输入%tmp%回车获得所有临时文件,全选删除即可 ......
  • .net core HttpClient
    .netcoreHttpClient使用之掉坑解析(一)-Jlion-博客园(cnblogs.com) ......
  • 使用StackExchange.Redis组件C#模糊删除模糊查找
     C#StackExchange.Redis模糊删除模糊查找 ///<summary>///模糊查找///</summary>///<paramname="key"></param>publicList<XJDataDll.Tag.Point>SelectTags(stringpattern){......
  • 页面引入css样式时,使用link和@import有什么区别
    css文件引入的方式有两种:1.HTML中使用link标签<linkrel="stylesheet"href="style.css/>2.css中使用@import@import"style.css"/*使用字符创*/@importurl("style.css")/*使用url地址*/link和@import区别link属于HTML标签,除了加载css外,还可以做很多其的他事,比......
  • Tomcat配置ssl证书
    1.生成ssl证书keytool-genkeypair-alias“tomcat”-keyalg“RSA”-keystore“D:\ssl\tomcat.keystore”-validity365002.将生成证书复制到tomcatssl文件夹下,修改tomcat配置,找到tomcat/conf/server.xml文件<Connectorport="8081"protocol="org.apache.coyote.http1......
  • C#_上传文件到服务器的temp文件夹
    C#_上传文件到服务器的temp文件夹1.写在*.aspx中<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="有效代码_上传文件到服务器的temp文件夹.aspx.cs"Inherits="web_page_ssc_上传文件到服务器的temp文件夹"%><!DOCTYPEhtml><htmlxmlns=&......
  • EMMC分区设置
    一、fdisk-l(查看盘符)二、fdisk/dev/mmcblk0(对EMMC进行分区设置) 1、p(查看当前的分区)2、d(删除分区)3、n(新建分区) 4、w(保存设置并退出) 三、mkfs.ext4(格式化分区) 四、mount /源地址  /目标地址(挂载分区) 五、df-h(查看可用分区) ......