Why the compiler decide 2.3 is double and not decimal?

后端 未结 4 943
庸人自扰
庸人自扰 2020-12-30 05:44

Why does the compiler decide that 2.3 is double so this code won\'t compile:

decimal x;
x = 2.3; // Compilation error - can not convert double to decimal.
x          


        
4条回答
  •  再見小時候
    2020-12-30 05:47

    2.3 is double. That is the language rules; any numeric literal with a decimal point in it is a double, unless it has a F suffix (float), or M suffix (decimal):

    x = 2.3F; // fine
    

    The compiler helpfully tells me this, too:

    Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type

提交回复
热议问题