PHP convert decimal into fraction and back?

前端 未结 14 939
面向向阳花
面向向阳花 2020-12-05 07:43

I want the user to be able to type in a fraction like:

 1/2
 2 1/4
 3

And convert it into its corresponding decimal, to be saved in MySQL,

14条回答
  •  渐次进展
    2020-12-05 08:07

    An approach would be to retrieve the decimal value and multiply it by 2, 3, 4 and so on until you get an integer number.

    However, I'd stick with the answer given by Derek. Guess what happens when a user inserts n/(n+1) with n high. Such an algorithm would have to scan all the numbers up to n+1. Not to mention it is likely you'll end up with approximation problems.

提交回复
热议问题