Possible Loss of Fraction

后端 未结 5 1608
慢半拍i
慢半拍i 2020-12-02 14:12

Forgive me if this is a naïve question, however I am at a loss today.

I have a simple division calculation such as follows:

double returnValue = (myO         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 14:37

    You're doing integer division if myObject.Value is an int, since both sides of the / are of integer type.

    To do floating-point division, one of the numbers in the expression must be of floating-point type. That would be true if myObject.Value were a double, or any of the following:

    double returnValue = myObject.Value / 10.0;
    double returnValue = myObject.Value / 10d; //"d" is the double suffix
    double returnValue = (double)myObject.Value / 10;
    double returnValue = myObject.Value / (double)10;
    

提交回复
热议问题