C# simple divide problem

前端 未结 9 1022
感动是毒
感动是毒 2020-12-11 01:40

I have this:

 double result = 60 / 23;

In my program, the result is 2, but correct is 2,608695652173913. Where is problem?

9条回答
  •  旧巷少年郎
    2020-12-11 01:50

    60 and 23 are integer literals so you are doing integer division and then assigning to a double. The result of the integer division is 2.

    Try

    double result = 60.0 / 23.0;
    

    Or equivalently

    double result = 60d / 23d;
    

    Where the d suffix informs the complier that you meant to write a double literal.

提交回复
热议问题