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

Add rounding to this script

New Here ,
Nov 08, 2012 Nov 08, 2012

Hi everyone,

I need to add rounding to this script. I need two versions, one for 1 decimal place and one for 0 decimal places. Can anyone help out?

#target illustrator

addMeasurements();

function addMeasurements() {

   

    var i, doc, imp, met, txtItems, txt, nub;

    doc = app.activeDocument;

    imp = doc.layers.getByName( 'Text' );

    met = doc.layers.add();  met.name = 'New';

         

    txtItems = imp.textFrames;

    for ( i = 0; i < txtItems.length; i++ ) {

        txtItems.duplicate( met, ElementPlacement.PLACEATEND )

    };

    txtItems = met.textFrames;

    for ( i = 0; i < txtItems.length; i++ ) {

        var nub = parseInt( txt = txtItems.contents ); // For Numbers only!!!!

       

        txtItems.contents = String( txt * 1.1 )

    };

};

Thanks in advance.

TOPICS
Scripting
1.5K
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
Adobe
Community Expert ,
Nov 08, 2012 Nov 08, 2012

What's the content of your textFrames?

Rounding: up or down or mathematical or other?

e.g. like this:

var x = 123.4567;

alert (Math.round (x*10)/10);

alert (Math.round (x));

Try it.

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
New Here ,
Nov 10, 2012 Nov 10, 2012

Hi. Thanks for the help. The text frames are numbers only, or numbers and 1 letter eg 10R Id like to have it round down for up to .49 and up from .5.

I'll gave it a go but couldnt get it to work.

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
Guru ,
Nov 10, 2012 Nov 10, 2012

What I use…

$.writeln( roundToDP( 1234.56789, 2 ) );

function roundToDP( n, dP ) {

 

          return Math.round( n * Math.pow( 10, dP ) ) / Math.pow( 10, dP );

 

};

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
Community Expert ,
Nov 10, 2012 Nov 10, 2012

Please show some examples of your numbers and a screenshot with visible layers palette..

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
New Here ,
Nov 10, 2012 Nov 10, 2012

Thanks for the replies. Excuse my lack of scripting knowledge. Mark thanks for the example, where do I insert that to the code you already gave me?

Heres a test file of what im using it on

https://www.dropbox.com/s/ko2v9dx89uh3wjk/script.ai

Thanks

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
Community Expert ,
Nov 11, 2012 Nov 11, 2012

Thanx for your example-file.

But what I really need is a file with numbers before and after using script. (Please CS3-conform)

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
Guru ,
Nov 11, 2012 Nov 11, 2012

The example is just a basic function… Just place it after the main function body in your script… Call it where you need it… here is an example… I changed your parseInt() to parseFloat() to deal with your decmial values… While this should wolk it still may NOT be what you want… i.e. it does nothing about keeping the alpha characters in your text frames the new numbers replace all content…

#target illustrator

addMeasurements();

function addMeasurements() {

  

    var i, doc, imp, met, txtItems, txt, nub;

    doc = app.activeDocument;

    imp = doc.layers.getByName( 'Numbers' );

    met = doc.layers.add();  met.name = 'New';

        

    txtItems = imp.textFrames;

    for ( i = 0; i < txtItems.length; i++ ) {

        txtItems.duplicate( met, ElementPlacement.PLACEATEND )

    };

    txtItems = met.textFrames;

    for ( i = 0; i < txtItems.length; i++ ) {

        var nub = parseFloat( txtItems.contents ); // now parseFloat()

 

                              nub = nub * 1.123456; // just to check the function rounds…

 

                              var rnub = roundToDP( nub, 2 );

      

        txtItems.contents = String( rnub ); // replaces all content…?

    };

};

//

function roundToDP( n, dP ) {

 

          return Math.round( n * Math.pow( 10, dP ) ) / Math.pow( 10, dP );

 

};

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
Community Expert ,
Nov 11, 2012 Nov 11, 2012
LATEST

Try this on a single textFrame with numbers and following Letter:

#target Illustrator

// RoundNumBeforeLetter.jsx

// http://forums.adobe.com/thread/1095744?tstart=0

// rundet Zahlen vor einem Buchstaben

var myDoc = app.activeDocument;

var myTxt = myDoc.textFrames[0];  //the first TextFrame in document

var myTxtCharLen = myTxt.characters.length;

var myTxtNum, j, LastChar = null;

for (i=0; i <=myTxtCharLen-1;i++) {

    myTxtChar = myTxt.characters.contents;

    if (SearchText(myTxtChar) == true) {

        j = i;

        myTxtNum = myTxt.contents.slice (0,j);

        myTxtNum = Math.round (myTxtNum);

        LastChar = myTxt.contents.slice (j);

        myTxt.contents = myTxtNum+LastChar;

    }

}

function  SearchText(x)

{

    var myRegExp  =  /(^-?[A-Z]$)/;

    return myRegExp.test(x);

}

Maybe it's not as elegant. On the contrary, it probably is "complicated and a long way around," but should work.

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