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

0 favourites
  • 14 posts
From the Asset Store
A collection of a mix of percussion and orchestral string loops, including 20 musical sketches.
  • Hello,I have a problem in process math calculate:

    How to convert a float like 0.33333333 to percent string "1/3"?

    like:

    0.333333333333 -> "1/3"

    0.666666666666 -> "2/3"

    and how to determine transfer Infinity number (ie. result of 1/0) to a special string like "NA"?

    thanks.

  • Have a look here: dec2fract.html

  • Decide how forgiving you want to be, then create some fun if statements!

    If x <.334 and x > .332 string = "1/3"

    if x = .25 string ="1/4"

    if x < .1668 and x > .1666 string = "1/6"

    do this for every possible fraction you care about.

    would be smartest to create a function to contain these checks as well.

    FUNction

    function string decimalToFraction(x){

    if if if ...

    return string;

    }

  • thanks. but the float is result of some calculating, ie. 2.333333 is result of 8/3.

    so that float number is indefinite. how can i transfer it to a frac string

    2.333333 -> 8/3?

    it cannot be solved by "if else" condition.

    blackhornet supply a good idea, is there a simple method or function? thanks more.

  • I am almost certain that there is no simple function. The simple method is as I described. Decide upon a precision threshold, and check the digits to the right of the decimal place. There is a method to truncate into an integer, so you can make two strings and concatenate them. so your 8/3 as 2.333333 becomes 2 and 1/3.

  • The reason is that .3333333333333333333 is not 1/3. It is an approximation of 1/3, and to go between the two needs some human judgement, e.g. IF statements.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • i mean that float is accurate of 1/3. the problem is how to transfer a repeating decimals (0.33333333) to fraction express (1/3) in order to show ?

  • Computers don't use that notation as they usually deal in decimals. Fractions are just our way of simplifying how we read it. So I doubt there is a "simple" way, but someone may have written a javascript library to handle that type of thing for some type of math learning app. So you may be ale to find one and write a plugin for it. But C2 most likely doesn't have a built in easy way as that isn't really a common gaming requirement.

  • thanks a lot. i'll try some method.

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

  • interesting problem

    decimalToFraction.capx

  • 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.

  • 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

  • It's a perfect algorithm! the above function solves my problem perfect!

    thanks a lot.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)