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.
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