Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Decimal value to fraction

Contributor ,
Mar 08, 2016 Mar 08, 2016

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

Screen shot 2016-03-08 at 8.46.46 PM.png

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

TOPICS
Scripting
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Mar 10, 2016 Mar 10, 2016

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

...
Translate
Community Expert ,
Mar 08, 2016 Mar 08, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 09, 2016 Mar 09, 2016

Hi Kahrel,

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

selva

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 10, 2016 Mar 10, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 15, 2016 Mar 15, 2016
LATEST

Hi Marc,

Thank you so much for this.

Selva

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines