why can't you assign a number with a decimal point to decimal type directly without using type suffix?

前端 未结 5 1995
借酒劲吻你
借酒劲吻你 2021-02-02 05:40

Why can\'t you assign a number with a decimal point to the decimal type directly without using type suffix? isn\'t this kind of number considered a number of type decimal?

5条回答
  •  天涯浪人
    2021-02-02 06:08

    Edit: I may have missed the last part of the question, so the overview below is hardly useful.

    Anyway, the reason you can't do what you're trying to do is because there is no implicit conversion between floating point types and decimal. You can however assign it from an integer, as there is an implicit conversion from int to decimal.


    You can, but you have to use this syntax (or do an explicit cast to decimal).

    decimal bankBalance = 3433.20m;
    

    and for floats it is

    float bankBalance = 3433.20f;
    

    default is double

    double bankBalance = 3444.20;
    

提交回复
热议问题