4 Replies Latest reply on Aug 11, 2013 3:25 AM by camilo umaña

# How to  convert 3.16 to 3.20 in tables? Round only the decimals?

In a recent post mr Trevor wrote a powerful script to round decimals but altering the preceding number:

23.45 was converted to 23.

23.78 to 24.

It is possible to script only the decimals not touching the integer?

,03 : 00

,06 : 10

,14 : 10

,18 : 20

,71 : 70

,77 : 80

Etc?

• ###### 1. Re: How to  convert 3.16 to 3.20 in tables? Round only the decimals?

Hi,

The way to reach accuracy is to multiply - round - devide back using 10, 100,.. etc

Sometimes you have to touch an integer

Example:

```a = 23.96;
b = (Math.round(a*10)/10).toString();
if (b.indexOf(".") == -1) b +=".00";
else b +="0";
```

Convert it to string is the way to have zeros at the end of "numbers"

Jarek

• ###### 2. Re: How to  convert 3.16 to 3.20 in tables? Round only the decimals?

... or with toFixed(2)

`var r = 23.96;`

`r = (Math.round(r*10)/10).toFixed(2); // 24.00`

Roland

• ###### 3. Re: How to  convert 3.16 to 3.20 in tables? Round only the decimals?

Hi, thank you.

As numbers are placed on tables, how to apply this script to the whole document?

It is necessary to make a list?

Or it may be used with a skeleton like this wrote by Trevor?

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

}

Yes, numbers between ,094-0,99 will afect the integer!

Thank you.

• ###### 4. Re: How to  convert 3.16 to 3.20 in tables? Round only the decimals?

I think your formula could be inserted in the skeleton provided by Trevor. I will intent. Thank you.