8 Replies Latest reply on Aug 25, 2013 6:14 AM by camilo umana

# It is possible to script a decimal approximation in Indesign?

In a file many decimal numbers should be approximate.

It is possible to script it?

Real    Required

22,90    23

21,87    22

26,47    26

11,93    12

29,16    29

• ###### 1. Re: It is possible to script a decimal approximation in Indesign?

Can use

Where myNumber is the number you need to check

```Math.round(myNumber);
```

There is a little problem of how .5 is handeled sometimes It will be rounded down and sometime up

If this is important you can do this:

To round up on .5

if (myNumber - parseInt (myNumber) == .5) myNumber = Math.ceil (myNumber) else myNumber = Math.round (myNumber);

To round down on .5

``` if (myNumber - parseInt (myNumber) == .5) myNumber = Math.floor (myNumber) else myNumber = Math.round (myNumber);
```

Trevor

• ###### 2. Re: It is possible to script a decimal approximation in Indesign?

Use findGrep to get the aray of numbers in the file

Process them as above and use changeGrep to change them

• ###### 3. Re: It is possible to script a decimal approximation in Indesign?

Trevor, rounding issues on "0.5" would only be a problem inside a Javascript. What you describe is what happens when a value seems to be reported as "0.5" but is actually "0.49" (for any number of decimals bigger than what was reported). Rounding this 'correctly' does not round the truncated value "0.5" but the original value instead.

For the Camilo's use, grabbing an exact number of decimals and processing these, it's not an issue, and he can simply use Math.round.

• ###### 4. Re: It is possible to script a decimal approximation in Indesign?

camilo umana wrote:

In a file many decimal numbers should be approximate.

It is possible to script it?

Only if you can define which numbers exactly should be rounded. Can you use any sort of color, position, size, font, style or anything else to find just these numbers, and none else? Should all numbers with a comma be rounded? Only numbers followed by a comma and then exactly two more digits -- no more, no less?

• ###### 5. Re: It is possible to script a decimal approximation in Indesign?

Hi Jongware

That's not what I understood from Marc #4 in http://forums.adobe.com/message/4402568#4402568

True that he's talking there about toFixed and not round but I think the same issue applies.to round

I don't have the array of machines to test.

Have you tested Math.round(.5) on the various different operating systems?

All that said my gut fealing is that you are correct.

• ###### 6. Re: It is possible to script a decimal approximation in Indesign?

Yes, i will make an array of those pertinent two comma decimals.

I know exactly which numbers must be rounded up.

Thanks a lot, Trevor, Jongware.

• ###### 7. Re: It is possible to script a decimal approximation in Indesign?

You might want to change the changeWhat grep depending on exactly what numbers you want to find but this is the basic idea to round all numbers with a "," instead of a "."

```var doc = app.activeDocument;
app.changeGrepPreferences = app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\d+\,\\d+";
var myNumberFinds = doc.findGrep(),
l = myNumberFinds.length;
while (l--)
{
app.changeGrepPreferences.changeTo = Math.round(myNumberFinds[l].contents.replace(/\,/,".")).toString();
myNumberFinds[l].changeGrep();
}

```

If you want to change numbers that have at least 1 but not more than 2 whole numbers then a comma then at least 1 and at most 2 fractional numbers then you could use for your find what line

app.findGrepPreferences.findWhat = "(?<=\\D)\\d{1,2},\\d{1,2}(?=\\D)";

That is providing that the story does not start or end with the number to be changed.

• ###### 8. Re: It is possible to script a decimal approximation in Indesign?

Trevor,

Thanks again for the whole script skeleton!

(I must confess that my programming skills are just for minor grep holydays tasks)

It is a precious piece!

Not to mention free of charge, Vat, taxes, etc. and delivered in minutes, doar to doar.

And is very very fast!

60 tables were processed in 4-5 seconds.