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

Number of Decimal Places

paulaeddy
Registered: May 14 2010
Posts: 22

Hello! I am attempting to input calculations in a pdf form to mimic an excel spreadsheet, however, I'm coming to the following issue:

PDF form result: 84.58333333333333 * 2 = 169.16666666666666

excel Result: 84.5833333333333 * 2 = 169.166666666667

The difference is resulting in a penny difference later on in a calculation. How can I get the pdf to calculate results just like the excel spreadsheet? Thanks!

My Product Information:
Acrobat Standard 9.3, Windows
jimhealy
Team
Registered: Jan 2 2006
Posts: 146
That is an error in representing the precision. The number is too precise for a double. Unfortunately excel does it differently than javascript, so you must mimic the misrepresentation in your PDF.

Jim Healy
FormRouter, Inc.
Check out our FREE Advanced Acroform Toolset:
http://www.formrouter.com/tools

Jim Healy, Founder & CEO FormRouter Inc.
Chapter Leader AUG RTP NC
http://www.formrouter.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can write a rounding function to round your answer to the desired number of decimal places for your computation.

Trying to represent decimal fractions in a binary system results in a number or irrational values.

JavaScirpt only supports about a limited number of significant digits. It appears that 13 decimal places is the limit for Acrobat JavaScript.

var nValue = 84.58333333333333 * 2console.println(nValue); function Round(nValue, iPlaces) {// round nValue to iPlaces decimal placesiPlaces = Math.floor(iPlaces);var nRound = util.printf('%,1 .' + iPlaces + 'f', nValue);return nRound; // pass back result;}console.println( Round(nValue, 13) );console.println(Round(nValue, 13).toString().length);

To get the same result as Excel, you can round to 12 decimal places.

In practice, I would add the Round function as a document level script and then call it in every calculated field to force the result of the computation to 12 decimal places.

George Kaiser

jimhealy
Team
Registered: Jan 2 2006
Posts: 146
It depends on what kind of numbers you expect. It's not really that you can round to 12 decimal places. That is only in the case that you have 3 digits preceeding the decimal point. The precision of a double is 15, so if the number cannot be converted to an exponential expression, you could only have a total of 15 digits total to the left and right of the decimal (assuming that they are all significant, and even this is a general rule). For example:
1234567890123.45
12345678901.2345
12.3456789012345

But you could also have:
0.0000000000000000000000005
and it would become:
5E-25

Probably the easiest thing to do would (if possible, and I don't know what kind of accuracy you need) be to round all numbers in your excel sheet to some reasonable level at each step and do the same on the PDF.

Jim Healy
FormRouter, Inc.
Check out our FREE Advanced Acroform Toolset:
http://www.formrouter.com/tools

Jim Healy, Founder & CEO FormRouter Inc.
Chapter Leader AUG RTP NC
http://www.formrouter.com

wdjudd
Registered: Mar 22 2011
Posts: 3
I use Adobe Acrobat 9 Pro. After reading this discussion I find myself very confused but I am not use to writing JavaScript. I am preparing a PDF form that contains a series of fields with numeric values that need to be summed and/or averaged. In the cell used for summing the values I select, Text Field Properties/Calculate/Value is Sum (+). The summed value has several decimal places but I only want one decimal place to the right. How do I do this? If I need to use JavaScript, where does it go? I know this is a very basic question but this is my skill level with Acrobat – Excel is a different story.

Thanks for you time to answer my question.

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can set the format to "Number" and specify the number of places. Note that this method rounds the result for display but reattains the full precision for calculations. So if you have a calculation the results in a value of 0.6666666666666666 and have a format display or 2 decimal places, then 0.67 displays, and if in a later field you access that field for a calculation, then 0.6666666666666666 is used for the calculation and not 0.67. So if you are multiplying by 1000 this would result in a value of 6,666.666666666666 using the computed result and 6,700.00 using the formatted value or a difference of 33.33333333333394 or an error of 0.5%. And for some calculations that can be a significant error.

Some of these issues are caused by trying to represent decimal fractions in a binary format.

For financial transactions it is best to round the results and use the rounded result in later calculations. Statistics tend to be more reliable if unrounded results are used.

If you need the result rounded then you need to use the custom calculation script, How to do (not so simple) form calculations by Thom Parker, and you might want to use a user written ronding function as there is a well known issue with JavaScirpt's rounding method, Entering Document Scripts by Thom Parker.The best rounding funciton I have found is from D.P. Story:

function Round (n, d) {
n = n.toString().replace(/,/g,"");
d = -1 * d
var m = Math.pow(10, d);
n *= m;
n = Math.round(n);
n /= m;
d = ( d > 0 ) ? d : 0;
n = n.toFixed(d);
return n;
}

George Kaiser

wdjudd
Registered: Mar 22 2011
Posts: 3
gkaiseril wrote:
You can set the format to "Number" and specify the number of places. Note that this method rounds the result for display but reattains the full precision for calculations. So if you have a calculation the results in a value of 0.6666666666666666 and have a format display or 2 decimal places, then 0.67 displays, and if in a later field you access that field for a calculation, then 0.6666666666666666 is used for the calculation and not 0.67. So if you are multiplying by 1000 this would result in a value of 6,666.666666666666 using the computed result and 6,700.00 using the formatted value or a difference of 33.33333333333394 or an error of 0.5%. And for some calculations that can be a significant error.Some of these issues are caused by trying to represent decimal fractions in a binary format.

For financial transactions it is best to round the results and use the rounded result in later calculations. Statistics tend to be more reliable if unrounded results are used.

If you need the result rounded then you need to use the custom calculation script, How to do (not so simple) form calculations by Thom Parker, and you might want to use a user written ronding function as there is a well known issue with JavaScirpt's rounding method, Entering Document Scripts by Thom Parker.The best rounding funciton I have found is from D.P. Story:

function Round (n, d) {
n = n.toString().replace(/,/g,"");
d = -1 * d
var m = Math.pow(10, d);
n *= m;
n = Math.round(n);
n /= m;
d = ( d > 0 ) ? d : 0;
n = n.toFixed(d);
return n;
}
Hi George:

Thanks for your assistance. Both the decimal format and rounding functions work as expected.

However, my problems are not yet solved. If this is not the place for this question please tell me and I will move it elsewhere. I have created a PDF form that contains data fields for each day of the week, Sunday - Saturday. I am trying to AVERAGE the data input for the days of the week that contain data, and have entered the 7 data fields to be averaged. As long as there is data in all 7 days the AVERAGE works as expected, 7 days of data summed / 7 days. However, if there is only data in 5 days, the summed data is still divided by 7 days, not 5 days. Is there a way to force the AVERAGE to divide by ONLY those fields that contain data so the AVERAGE is always appropriate for the number of days containing data?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Yes, but you need to write a custom script to do that. The simple provided built-in function assumes all the fields are used for the computation, and this is not always the case. The built-in functions for sum, average, and product treat empty or null fields as a zero value.

See Counting, Summing, Averaging Numeric values only for a smaple form with the necessary scripts.The sum, count and average are computed by using document level functions.


George Kaiser

wdjudd
Registered: Mar 22 2011
Posts: 3
Thanks George . . . I may have to invest some time with this issue.