6 Replies Latest reply on Apr 25, 2011 12:41 PM by gkaiseril

# Rounding out numbers

Hi:

I have a form that is called Scoping Cost Summary Tool.  There is a column where we are totaling up the different work items.  In each section, there is a subtotal and a rounded subtotal.  The Excel file shows the calculation as:  =ROUNDUP(H32,03).  Is there a javascript available that will do the same as this does in Excel?

Thanks for any assistance.

Connie

• ###### 1. Re: Rounding out numbers

You can use the Math.round method. In your example, it appears as though you want to round with three digits to the right of the decimal point. Is that correct? If so, first multiply the amount by 1000, round the result, and divide that by 1000. There's a lot of information on the Math.round function out there that you should be able to find with a search. If you get stuck with the script, post again with what you have.

• ###### 2. Re: Rounding out numbers

The 'Roundup' function in Excel rounds the number to the next higher value for the number of decimal places. When the number of digits for the Excel function is zero, the Excel Roundup function is the same as the JavaScript Math.ceil method.  To add the number of digits for the decimal places, one needs to write a user function that adjust the decimal place by multiplying by an exponential power of 10, rounding that value up to the next integer, and end dividing that round number by same value used to move the decimal place to restore the decimal place to the intended location.

function Roundup(fValue, nDec) {

// compute value for decimal adjustment

// round to next integer

fValue = Math.ceil(fValue);

// return rounded up number with fixed decimal location

} // end Roundup function

You then can call this function

var H32 =  123.45611;

Roundup(H32,  3)

To get a return value of 123.457

This code can not be used in the simplified field notation on the Calculation tab.

JavaScript also has a Math.floor function that truncates a value to the closest lower integer. This would be like the ROUNDDOWN function in Excel.

There is also a round function in JavaScript, but there is some issues with the way it rounds decimals, but with additional coding one can work around that problem and create a reliable decimal rounding function.

The above code is for forms created in Acrobat and not LiveCycle Designer. LiveCycle Designer has the Ceil and Floor functions built in to the application.

• ###### 3. Re: Rounding out numbers

Hi, thanks for your reply.  I used the code you sent me, and it doesn't work.  (See below)

function Roundup(fValue, nDec) {
fValue = Math.ceil(fValue);
}
Roundup(123.4567, -3)
var Rsubtotal.value =  123.45611

Can you tell me what I did wrong?

Thanks

Connie

• ###### 4. Re: Rounding out numbers

If you you want the variable "Rsubtotal" to contain the value of 123,4567 rounded up to the nearest 1000 then you need to set the value of the variable to returned value from the Roundup function:

var Rsubtotal =  Roundup(123.4567, -3)

Variables in JavaScript have no proprieties, only values.

If you are trying to set a field value to the result of the function the code will depend upon where you place the code.

In the custom calculation script for the field named "Rsubtotal"

event.value =  Roundup(123.4567, -3);

If in another location other than that field;

this.getField("Rsubtotal").value =  Roundup(123.4567, -3);

You place the code for the function in the Document level JavaScript location for the PDF.

• ###### 5. Re: Rounding out numbers

What I'm trying to do, is  have one Column/field called SubTotal, and one underneath that Column/field called RSubtotal.  The RSubtotal needs to round off the Subtotal's total.  I'm sorry to be such a dork about this, I've never used this type of javascript before and I'm a little confused.  Would I use this one?

event.value =  Roundup(123.4567, -3);  What it does is put 1,000.00 in the RSubtotal.  Thanks so much for your patience and assistance.  Connie