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

Rounding Issues

jhosch
Registered: Feb 18 2010
Posts: 10
Answered

Hello,

I am trying to make a number round, and then multiply it by another number. I have two fields being divided and multiplied by 100, to make a percent. However, the percent can only be three decimal places. This number then must be multiplied by another field. I am able to get the fields to display a rounded value, but the output is not correct. I have tried to use the Round( n1, 1) function, however it doesn't seem to effect the outcome.

Can somebody please help me?

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
In what way is the answer wrong?

Did you want to use the rounded percentage in the computation? Or not?

Is the result off by a factor of 100?

Maybe you only need limit the display of the percentage computation to show only 2 trailing digits but use the result of the division in the computations.

George Kaiser

jhosch
Registered: Feb 18 2010
Posts: 10
Well, the answer is essentially correct. It just uses the entire not rounded number. But, I do need to use the rounded number in the computation. The display is showing only the rounded number, which is great, however, the computation needs to use that number as well.

Thank you for your help!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Then in the calculation you use the 'Round' function to adjust the computation of the percentage.

So if you have fields 'A' and 'B" and you want the percentage that 'A' is of 'B' rounded to 1 decimal place in field 'C', the FormCalc script could be:
i f ( HasValue(B) & (B <> 0) ) then//  if there is a divsor and it is not zero then compute decimal value to 3 places and adjust for displayRound( (A / B) , 3) * 100elsenullendif

George Kaiser

nh39
Registered: Feb 19 2010
Posts: 7
The following code works for me. It truly gets rid of the decimal, rather than masking it by showing the display value.
This example has 3 numeric fields, called num1, num2 and ans.
I would also suggest you set user entered - optional for num1 and num2 and protected for ans.

// javascript for num1
varRem = 0; // Will hold the value of the unwanted decimal digits
varAns = 0; // Will calculate the unadjusted quotient of num1 and num2
if (!this.isNull && !num2.isNull && num2.rawValue != 0)
{
varAns = this.rawValue / num2.rawValue * 100; // Regular division
varRem = varAns % 1; // Taking all of the decimal digits
ans.rawValue = varAns - varRem; // Rounding up
if (varRem >= 0.5) // Adjusting the rounded up number
{
ans.rawValue = ans.rawValue + 1;
}
}//javascript for num2
varRem = 0; // Will hold the value of the unwanted digits to the right of the number
varAns = 0; // Will calculate the unadjusted quotient of num1 and num2
if (!this.isNull && !num1.isNull && this.rawValue != 0)
{
varAns = num1.rawValue / this.rawValue * 100; // Regular division
varRem = varAns % 1; // Taking all of the decimal digits
ans.rawValue = varAns - varRem; // Rounding up
if (varRem >= 0.5) // Adjusting the rounded up number
{
ans.rawValue = ans.rawValue + 1;
}
}
jhosch
Registered: Feb 18 2010
Posts: 10
Thank you both for your help! I had something very similar to the code that gkaiseril posted. But, that code worked perfectly! I really appreciate your help!