Convert Decimal number into Fraction

前端 未结 6 1835
挽巷
挽巷 2021-01-15 00:50

I am trying to convert decimal number into its fraction. Decimal numbers will be having a maximum 4 digits after the decimal place. example:- 12.34 = 1234/100 12.3456 = 1234

6条回答
  •  盖世英雄少女心
    2021-01-15 01:07

    If your floating point number is x, then the numerator of the fraction over 10000 will be the integral part of (x + 0.00005) * 10000. It's up to you whether you want to reduce the fraction to simplest terms (i.e. divide out by the gcd of the numerator and denominator).

提交回复
热议问题