Number to fractional number

Hi all

I have one number. I want to convert that it to fractional

For ex: I have a=0.02 so if I convert that number into fraction then it will be 1:50

So I dont want to express it in fractional feet and inches I want express it in SI fractional unit. Please let me know how to convert a number into fraction (SI fraction i.e 1:ratio) like explained above.

@shashank.baganeACM I do not think you can get it other than a string, maybe someone else can prove me wrong:

3 Likes

Looks like there’s a module for converting decimals to a numerator and denominator, but otherwise I believe @Elie.Trad is correct. There’s no fractional number type so the best you can do is a string. This also allows you to round to a fractional precision if the decimal is irrational or otherwise results in an “awkward” fraction.

3 Likes

Thank you so much @Elie.Trad

1 Like

and if I want reverse output

I mean if I have a string 1:50 and if I want to get number 0.02 then how to do that ? Can you please tell me

@shashank.baganeACM maybe something like this:

2 Likes

Thank you @Elie.Trad

1 Like

DecimaltoFraction.dyf (61.4 KB)
I made this.

In case you’re interested in what I did, in words, I started by subtracting the input number value from the floor to separate the decimal value from the whole integer. Then, I multiplied the decimal value by each fractional denominator value (2,4,8,16,32,64,128,256). I had it take the first value that was a whole number so that the fraction is simplified. If none of the values were whole numbers, I set it to round the last value of the specified denominator value. By default, it will return fractional values to the nearest 1/256, but you can specify which denominator to round to. Hope it helps!

1 Like