Skip to main content
Known Participant
May 21, 2013
Answered

Changing decimal values to fractions

  • May 21, 2013
  • 2 replies
  • 7440 views

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.

This topic has been closed for replies.
Correct answer Jongware

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));

}

alert (size);

2 replies

Jongware
Community Expert
Community Expert
May 22, 2013

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);

alert (size);

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

CarlosCanto
Community Expert
Community Expert
May 22, 2013

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"

StanStillAuthor
Known Participant
May 23, 2013

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.

Inspiring
May 21, 2013

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.

This is an online version of the guide for CS5: http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/JavaScriptToolsGuide_CS5.pdf

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.

StanStillAuthor
Known Participant
May 22, 2013

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

StanStillAuthor
Known Participant
May 22, 2013

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


Looks like I have homework to do...