These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Rounding to 2 decimal places without rounding up

Spiderman
Registered: Feb 23 2011
Posts: 3
Answered

I have a dilemma wherein the following returns no cents
 
var m1 = this.getField("miles1");
var pm1 = this.getField("permile1");
event.value = Math.round(m1.value * pm1.value);
 
and
 
var m1 = this.getField("miles1");
var pm1 = this.getField("permile1");
event.value = m1.value * pm1.value;
 
returns the answer always rounded up on the cents.
 
I need to cut the calculation off at the second decimal point without any rounding. Where would I put the parameters in
 
event.value = Math.round(m1.value * pm1.value);
 
to give the answer of 205.6 miles * .51 cents / mile = $104.856 chopping off the 6 and giving me the $104.85?
 
Thanks in advance.

My Product Information:
Acrobat Pro 9.4.2, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The 'Math.round()' method rounds the nearest integer and is clearly not what you want to use.

Have you looked at the "parseInt" object or the 'Math.floor' method


// document level script using parseInt to truncate to nDec places
function Int(nValue, nDec) {
// parseInt nValue to nDec places
var nAdj = Math.pow(10, parseInt(nDec))
return parseInt(nValue * nAdj) / nAdj;
}


// document level script using Math.floor to truncate to nDec places
function Floor(nValue, nDec) {
// truncate a value at nDec places
var nAdj = Math.pow(10, Math.floor(nDec))
return Math.floor(nValue * nAdj) / nAdj;
}


// custom calculation script to truncate computation to nDec places
var m1 = this.getField("miles1"); // mileage
var pm1 = this.getField("permile1"); // per diem for mileage
var amt = m1.value * pm1.value;
event.value = Floor((m1.value * pm1.value), 2);

// some debugging information
// open and clear the console
console.show();
console.clear();
// print the computation
console.println(m1.value + " * " + pm1.value + " = " + amt);
// truncate to 2 decimal places parseInt
console.println('Int(' + amt + ') = ' + Int(amt, 2));
// floor to 2 decimal places Math.round
console.println('Floor(' + amt + ') = ' + Floor(amt, 2));

George Kaiser

Spiderman
Registered: Feb 23 2011
Posts: 3
Accepted Answer
gkaiseril,
Thanks so much for such a quick response.

I tried

var m1 = this.getField("miles1"); // mileage
var pm1 = this.getField("permile1"); // per diem for mileage
event.value = Math.floor((m1.value * pm1.value),2); //In the Acrobat Custom calculation script I have to use Math.floor

The method does not recognize the ,2 for the number of places to stop at. It just cuts off at the integer also.

This may be because I am trying to do it in a Custom calculation script in an Adobe Acrobat form.


gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You need to use the "Floor" document level script and call that script function. The function adjust the decimal places to temporarily shift the decimal place so the number will truncate at the correct location, applies the truncation action, and then shifts the decimal point back to its starting location. You can not just drop this function, change the code to how you think it should be and expect the code to work. The function "Floor" is a modification of the "Math.floor" method. Capitalization makes it a different action.


// custom calculation script to truncate amount to 2 decimal places
var m1 = this.getField("miles1"); // mileage
var pm1 = this.getField("permile1"); // per diem for mileage
var amt = m1.value * pm1.value;
var nAdj = Math.pow(10, 2); // decimal adjustment
event.value = Math.floor(m1.value * pm1.value * nAdj) / nAdj;

The use of the document level function, allows one to reuse the code in many locations and have one set of code statements to edit if any changes are needed.

George Kaiser

Spiderman
Registered: Feb 23 2011
Posts: 3
Thanks!!! That is exactly what I need. A Billion Good Guy Bucks to you!!