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

前端 未结 5 1998
借酒劲吻你
借酒劲吻你 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 05:56

    Your answer consists of two important points:

    1. All numerical literals with a decimal point are inferred to be of type double by the C# compiler, consequently, 3433.20 is a double by default.

    2. double numbers do not implicitly convert to decimal because although decimal is more precise than double it covers a shorter range so overflow is possible during a cast from double to decimal.

    double's range: ±(~10^−324 to 10^308) with 15 or 16 significant figures.

    decimal's range: ±(~10^-28 to 10^28) with 28 or 29 significant figures.

提交回复
热议问题