Convert Decimal number into Fraction

前端 未结 6 1842
挽巷
挽巷 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:02

    this is an interesting question. I think you might be better off starting with reading about the multiples ways of calculating the "greatest common divisor" ( http://en.wikipedia.org/wiki/Greatest_common_divisor is a good source ).

    Implement a quick&dirty algorithm that makes those calculations as you would do with a pen and paper then look into how doubles are represented (sign, exponent, mantissa) and improve your algorithm to take advantage of this representation.

    sadly, there's not much more I can do without writing your piece of code.

提交回复
热议问题