how to convert float like 0.333333 to string "1/3"

Get help using Construct 2

Post » Sun Mar 10, 2013 5:28 pm

This javascript example will get you on your way if you want to make a plugin to do it.

B
6
S
1
G
1
Posts: 70
Reputation: 702

Post » Sun Mar 10, 2013 8:23 pm

interesting problem
decimalToFraction.capx
Last edited by Yann on Sun Sep 14, 2014 7:27 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Mar 11, 2013 12:11 pm

Above example is just what i want.but it has some restricts:
it can process 1 recursive digit such as .3333..., but cannot process 2 or more recursive digit (maybe 6 is enough). for example: 0.142857142857...->1/7 ,0.076923076923...->1/13, which has 6 recursive digit. It also need skip leading digit that may not be recursive, e.g. 0.4777->43/90 ,that leading 4 is not recursive.

Maybe more work needed ,it's not a simple task anyway .

Yann's "DecimalToFraction" is really very useful for me . maybe it could be put plugin to enhance maths function.

Thanks a lot.
B
3
Posts: 6
Reputation: 337

Post » Mon Mar 11, 2013 6:11 pm

Interesting problem indeed.

Here's another algorithm that works well but is a bit slower:
It finds 1/7 and 1/13 quite well.
http://dl.dropbox.com/u/5426011/examples17/dec2frac.capx

It's based on the forth answer here:
http://stackoverflow.com/questions/5124743/algorithm-for-simplifying-decimal-to-fractions
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Tue Mar 12, 2013 1:04 pm

It's a perfect algorithm! the above function solves my problem perfect!
thanks a lot.
B
3
Posts: 6
Reputation: 337

Previous

Return to How do I....?

Who is online

Users browsing this forum: blackhornet, Solomon and 27 guests