4 Replies Latest reply on Mar 15, 2016 11:56 PM by selvam214

# Decimal value to fraction

Hi,

How to change the decimal values (inches) to fraction method, samples & screenshot here (left is tool output, right is needed method). The values need to change pica format also (Para Indent: 1p2), kindly guide on this.

Trim: 4.1875” x 6.75”                      ==>       Trim: 4 3/16" x 6 3/4"

Para Indent: 1.16666666666667    ==>       Para Indent: 1p2

Sample code here:

var width = app.activeDocument.documentPreferences.pageWidth;

var height = app.activeDocument.documentPreferences.pageHeight;

var myTrim = "Trim: "+width+"\" x "+height+"\"\r";

//struggle to convert decimal to fraction

• ###### 1. Re: Decimal value to fraction

The second item in the list produced by googling "convert decimal inches to fraction" is Decimal Inch to Usable Fractions Converter. The script is on the page (press Ctrl+U in FireFox).

• ###### 2. Re: Decimal value to fraction

Hi Kahrel,

Thanks for this link, i'll check and get back with the result.

selva

• ###### 3. Re: Decimal value to fraction

Hi selva,

Try this:

```var toFrac2 = function(/*str|num>0*/x,/*4|8|16|32|64...*/precision,  p,r)
{
precision || (precision=64);
x += '';

if( -1 == (p=x.indexOf('.')) ) return x||'0';

r = x.substr(1+p).replace(/0+\$/,'');
x = x.substr(0,p);

r = Math.round(precision*parseInt(r,10)/Number('1e'+r.length));

if( !r ) return x||'0';

x = (+x) ? (x+' ') : '';

while( !(1&r) ){ r>>>=1; precision>>>=1; }

return x + r + '/' + precision;
};

alert( toFrac2(1.5) );         // =>  1 1/2
alert( toFrac2(".025") );      // =>  1/32
alert( toFrac2("3.426897") );  // =>  3 27/64
alert( toFrac2(3.426897) );    // =>  3 27/64
alert( toFrac2(4.1875) );      // =>  4 3/16
alert( toFrac2(6.75) );        // =>  6 3/4
alert( toFrac2(5.00001) );     // =>  5
```

@+

Marc

• ###### 4. Re: Decimal value to fraction

Hi Marc,

Thank you so much for this.

Selva