8 Replies Latest reply on Feb 1, 2013 7:07 AM by gkaiseril

# Round Number

as a value rounded to nearest 1000 example: 2500 = 3000, 2400 = 2000
event.value = Math.round (this.getField ("cas93"). value * (this.getField ("porAran"). value / 100

• ###### 1. Re: Round Number

This function will do it for you:

function roundToNearestThousand(x) {

var d = x % 1000;

if (d<500) return (x - d);

else return (x + (1000-d));

}

You can use it like this:

roundToNearestThousand(2400) // will return 2000

roundToNearestThousand(2500) // will return 3000

roundToNearestThousand(2900) // will return 3000

• ###### 2. Re: Round Number

not work, casilla1 = 5%, casilla2 = 250000 = 1250 = value is 13,000, then:

function roundToNearestThousand (this.getField ("casilla2"). value * (this.getField ("casilla1"). value / 100) {
var d = x% 1000;
if (d <500) return (x - d);
else return (x + (1000-d));
}

can you tell me if what I'm doing wrong?

• ###### 3. Re: Round Number

Don't divide the percentage field by 100. Its value is by definition

between 0 and 1, so what you see as 5% is actually 0.05.

• ###### 4. Re: Round Number

Also, you're not using the function correctly

You need to leave the function as it is, and use it like this:

var v = +this.getField ("casilla2").value * +this.getField ("casilla1").value;

event.value = roundToNearestThousand(v);

• ###### 5. Re: Round Number

excuse me I do not understand, look at the following code and income get error in line 2

var v = +this.getField ("cassilla2").value * +this.getField ("casilla1").value;

event.value = roundToNearestThousand(v){

var d = v % 1000;

if (d<500) return (v - d);

else return (v + (1000-d));

}

;

• ###### 6. Re: Round Number

It would have to be something like this if you want to do it that way:

// Custom Calculate script for text field

function roundToNearestThousand(v) {

var d = v % 1000;

if (d < 500) return (v - d);

else return (v + 1000 - d);

}

var v = +this.getField ("cassilla2").value * +this.getField ("casilla1").value;

event.value = roundToNearestThousand(v);

The function should really be defined in a document-level JavaScript and the custom calculation script for the field would be what try67 posting in response #4 above.

• ###### 7. Re: Round Number

great works and thank you very much, it is possible to tell me where I can learn more of the Acrobat JavaScript APIs.

Thousand and thousand thanks for the attention and collaboration

• ###### 8. Re: Round Number

A more general solution can be found at Grade School Sample Files by D.P. Story. In the sample instruction file is a rounding function that eliminates the rounding error found with JS's Math.round method and can round either to the left or right of the decimal point.