How can I detect if a float has a repeating decimal expansion in C#?

前端 未结 6 1962
一个人的身影
一个人的身影 2021-01-12 00:16

I simply need to know how I can detect repeating decimal expansion in floats.

Example:

0.123456789123456789

The repeating portion of the number would

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 00:30

    I don't think that there's solution in general (at least, with float/double):

    • period can bee too long for float (or even double);
    • float/double are approximate values.

    E.g., here's a result of division (double)1/(double)97:

    0.010309278350515464
    

    Indeed, it is a repeating decimal with 96 repeating digits in period. How to detect this, if you only have 18 digits after decimal point?

    Even in decimal there's not enough digits:

    0.0103092783505154639175257732
    

提交回复
热议问题