How can I divide two integers to get a double?

后端 未结 5 1006
谎友^
谎友^ 2020-11-22 08:39

How do I divide two integers to get a double?

5条回答
  •  一个人的身影
    2020-11-22 09:08

    Complementing the @NoahD's answer

    To have a greater precision you can cast to decimal:

    (decimal)100/863
    //0.1158748551564310544611819235
    

    Or:

    Decimal.Divide(100, 863)
    //0.1158748551564310544611819235
    

    Double are represented allocating 64 bits while decimal uses 128

    (double)100/863
    //0.11587485515643106
    

    In depth explanation of "precision"

    For more details about the floating point representation in binary and its precision take a look at this article from Jon Skeet where he talks about floats and doubles and this one where he talks about decimals.

提交回复
热议问题