13 Replies Latest reply on May 24, 2013 12:13 PM by CarlosCanto

# Changing decimal values to fractions

Does anyone know of a good way to convert decimal values to fractions for use in inch measurements? I have a script that works great but it returns values like 5.5638 and I'd rather it return the closest fraction answer like 5 9/16. 1/16ths would be great, but I'd settle for 1/8ths.

I started trying to come up with a large if with lots of > and < conditions, but I figured if anyone has already done this it might be better not to reinvent the wheel.

Thanks.

• ###### 1. Re: Changing decimal values to fractions

Hi StanStill

In the JavaScript tools guide look at the page 231. You can learn about UnitValue. All the avaliable options for conversion is described there.

If you have Adobe softwares installed, you can find it also in your computer, In Windows, it´s in C:/Program Files (x86)/Adobe/Adobe Utilities - CS6/ExtendScript Toolkit CS6/SDK folder.

Best Regards

Gustavo.

• ###### 2. Re: Changing decimal values to fractions

Thanks Gustavo,

I'm new to JavaScript, and scripting/programming in general, so I'm struggling a bit here, but I don't see in the guide how to set your own unit. Since I am trying to get it to write in the archaic 1/16th inch increments used in the US. So if I could set a unit as "/16th inch" = 4.5pt that would be great. Then I guess I would need to reduce it somehow so that if I get a value like 67/16th inch I could get the mixed fraction 4 3/16th inch or 8/16th inch would become 1/2 inch.

Cheers

• ###### 3. Re: Changing decimal values to fractions

the way they do it in this page, take a look at the source code and adapt it to your needs...from the author...

```<title>Fast Decimals to Fractions</title>
<script type='text/javascript'>
// No license. Free to copy, and provided inline to make it easy to copy.
```

http://www.mindspring.com/~alanh/fracs.html

• ###### 4. Re: Changing decimal values to fractions

Looks like I have homework to do...

• ###### 5. Re: Changing decimal values to fractions

yey!!

post back if you get stuck.

• ###### 6. Re: Changing decimal values to fractions

Stan, Carlos, you're both overthinking it.

Consider: accuracy in 1/16ths is merely Round(value*16)/16. The following script is tested using InDesign, but (for once!) I'm pretty convinced the maths do not need to be translated between the two "Suite" programs.

```Factor = 16;
size = app.selection[0].geometricBounds[3] - app.selection[0].geometricBounds[1];
size = String(Math.floor(size))+' '+(Math.round(Factor*size) % Factor)+'/'+String(Factor);
```

The first line just sets a baseline 'unit'. You can replace it with any positive integer -- fancy, measurements in 1/13ths! -- and even use '100' or '1000' to check it actually rounds correctly.

The second line is (oh okay) an InDesign specific construction, to grab the 'width' of a selected object. Needed something like that, because nowhere you say where you get your dimensions from.

The third line is where the Magic™ happens. (Note that you can replace the variable 'Factor' with your preferred sub-unit of '16'.)

The fourth line is to show that it works -- it echos the calculated string back to the screen.

(Yes: the result is "a string". Javascript cannot express "9 3/16" in a variable as a numerical result. So you cannot add "5/16" to this and expect it to change to "9 8/16", or "9 1/2" or something similar.)

• ###### 7. Re: Changing decimal values to fractions

StanStill wrote:

[..] if I could set a unit as "/16th inch" = 4.5pt that would be great. Then I guess I would need to reduce it somehow so that if I get a value like 67/16th inch I could get the mixed fraction 4 3/16th inch or 8/16th inch would become 1/2 inch.

Stan, maybe you should explain where and how you plan to use this 'measurement unit'.

My script shows how to easily get a string representation from a real number, but the inverse is a bit more work.

Also: you do realise that you cannot use this system inside Illlustrator itself? My script would work for showing the size of a selected object, but you cannot copy that value and paste it back into Illy's measurement fields -- that needs the reverse, calculating back a whole number+fraction into decimals.

• ###### 8. Re: Changing decimal values to fractions

nice and simple jw, if all the OP needs is to break it down to 16th's...but I would venture to guess the OP needs to convert 1.5 to 1 1/2" instead of 1 8/16"

• ###### 9. Re: Changing decimal values to fractions

Yes. This is basically for the sake of a proof sheet that shows measurements for a print. Because the print is done with different sized print cylinders depending on the art, it repeats at different intervals. So it would save me a lot of trouble if the script would run and calculate the width of the print, and the distance between prints. I have that part of the script working great and rounding the measurements to hundredths, but just to make my life more difficult I've decided that I want to see if I can get it to return fractions instead of decimals.

Quite frankly, I should probably just declare victory and tell everyone that we are switching to hundredths of an inch (like training wheels for the metric system), but that would involve me admitting that I have written a script that will allow me to process even more work. If I could get the script to do the fraction part, then I can pretend that I am still doing this manually and enjoy the extra hour every day.

• ###### 10. Re: Changing decimal values to fractions

CarlosCanto wrote:

nice and simple jw, if all the OP needs is to break it down to 16th's...but I would venture to guess the OP needs to convert 1.5 to 1 1/2" instead of 1 8/16"

Still not biggie Start with 1/16ths, and while the result an even number you can divide both into two.

```Factor = 16;
size = app.selection[0].geometricBounds[3] - app.selection[0].geometricBounds[1];
factorizedFraction = Math.round(Factor*size) % Factor;
if (factorizedFraction)
{
while (~(factorizedFraction | Factor) & 1)
factorizedFraction >>= 1, Factor >>= 1;
if (Math.floor(size) == 0)
size = String(factorizedFraction)+'/'+String(Factor);
else
size = String(Math.floor(size))+' '+String(factorizedFraction)+'/'+String(Factor);
} else
{
size = String(Math.round(size));
}