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

Multiplication Rounding up

Brunoboy
Registered: Nov 28 2008
Posts: 27

Hi,

Please help. I have spent countless hours searching in the Acrobat forums and blogs but I cannot find the answer to my multiplication problem.

I am trying to multiply two fields but the result always produce a figure that is rounded up, which I do not want. I want the actual result to show in the field without the rounding up of the figure.

Ex.: $305.56 x 36 is $11,000.16 but the result in the field shows $11,000.00

Can someone help me please?

My Product Information:
Acrobat Pro 9.1.1, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
What is the calculation script you are using?

What is the display format for the field with the product of the mulitiplicaiton?

Both Acrobbat and LiveCycle Designer both round to the selected dipaly format.

George Kaiser

Brunoboy
Registered: Nov 28 2008
Posts: 27
Thank you for replying so rapidely.

I am using the following Custom Calculation Script:

event.value = this.getField("Amt3d").value *
this.getField("LeaseTerm").value;

(Amt3d value in the field being $305.56 and LeaseTerm value in the field being 36)

I tried to use the simple option "Value is the product of "Amt3d" * "LeaseTerm" but it won't work and the result is still $11,000.00" instead of the actual multiplication result of "$11,000.16" (which I want).

I have Acrobat 9 Pro and I am using Acrobat (not LifeCycle).

Again, all I want is for the actual result of $11,000.16 to display (and not $11,000.00).

I hope this is sufficient information for you to help me. Thank you in advance.

Bruno
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
And what are the format options for the result field?

That is type, number of decimals, currency symbol, etc. All of this data will be on the "Format" tab.

Acrobat JavaScrpt uses the inputted value for the calculation, but will round up or down for the number of decimal places indicated under the 'Number Options' of the "Format' tab. This result is independent of the calculation method used.

George Kaiser

Brunoboy
Registered: Nov 28 2008
Posts: 27
Hi again,

The format options for the result field are:

Select format category: Number
Decimal places: 2
Separator Style: 1,234.56
Currence Symbol: None

Locked: Not checked

I am completely puzzled. you indicate that Acrobat JavaScript uses the inputted value for the calculation but will round up up or down for the number of decimal places indicated under the number option.

How can this be changed so the actual result of the multiplication appears in the field (without the rounding up).

Thanks again for looking into this. I am desperate because my client demands that accurate numbers appear in the fields and not rounded up numbers.

I hope you can assist me in finding an answer to this dilemma.

Bruno
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You could make the form field a text field and then create of formatted string within the 'Custom calculation script'.
event.value = event.value = util.printf('%,0 11.2f', (this.getField("Amt3d").value * this.getField("LeaseTerm").value) );// some more detailed informationconsole.show(); console.clear();console.println('Inputted values:');console.println('Amt3d: ' + this.getField("Amt3d").value);console.println('LeaseTerm: ' + this.getField("LeaseTerm").value);var product = this.getField("Amt3d").value * this.getField("LeaseTerm").value;console.println('\nProduct of Amt3d and LeaseTerm: ');console.println('No foramtting: ' + product);var productRound4 = util.printf('%,0 11.4f', product)console.println('Formatted to 4 decimal palces: ' + productRound4);var productRound2 = util.printf('%,0 11.2f', product)console.println('Formatted to 2 decimal palces: ' + productRound2);var productRound0 = util.printf('%,0 11.0f', product)console.println('Formatted to 0 decimal palces: ' + productRound0);

Try different values for your input and observe what happens.

The inputted values are rounded.

George Kaiser

Brunoboy
Registered: Nov 28 2008
Posts: 27
Hi again,

Thank you very much for trying so hard. You are simply wonderful and I really appreciate your time and effort.

I followed your suggestions very closely but unfortunately, nothing works.

I was not able to get the exact result value of the multiplication of the two fields (without the rounding up).

I don't know if I am asking too much from you or anyone else at this at this point but as a challenge to you, perhaps you could try it yourself by simply creating three entry fields with the values below and try to figure out a way to obtain the result with the rounding up:


$305.56
* 36
_______
$11,000.00 (but the actual result of the multiplication should be $$11,000.16)

And if you cannot figure it out, would you be good enough to suggest what else I could do or who else I could contact to resolve this issue.

I am sure everyone else doing simple multiplications are having the same issue and there must be an easy solution to this dilemma.

Bruno
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi Brunoboy,

I'm not sure what the problem is because I just did this on both Acrobat 8.1 Pro and 9.1 Pro and it works fine for me with no custom scripts added at all. I see $11,000.16 in the third text field when I use "Value is the Product of" the first two fields. I used the same format settings you mention above and I tried both using and not using the currency symbol.

Maybe you could remove all formatting from the field and then try it again- what do you get then?

Hope this helps,

Dimitri
WindJack Solutions
www.pdfscripting.com
www.windjack.com
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Are you using the "math.Round()" method anywhere?

George Kaiser

Brunoboy
Registered: Nov 28 2008
Posts: 27
Hi gkaiseril and Dimitri,

Thank you again for keeping on top of this one. No, I am not using the "math.round()" method anywhere.

Dimitri, I created another form and tried the simple multiplication, and it does work properly as you mentioned. However, an the form I need to create for a client, the same result keeps appearing and I am not able to get the exact result value of the multiplication of the two fields (without the rounding up).

As suggested, I removed all formatting from the field (actually, I deleted the field and re-created from scratch), but no success. The result of the multiplication is still being rounded up.

Any other suggestions?

Bruno
brunoboy [at] cogeco [dot] ca
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Post the file so the people here can inspect it.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Brunoboy
Registered: Nov 28 2008
Posts: 27
Hi Dimitri, gkaiseril, and try67,


Thank you to all of you for trying so hard. You have no idea how I appreciate your input. Actually, I think you may have stumbled on something that may be causing the rounding up problem in hope that you may have an easy solution.

Here is the calculation (and what I just discovered in the background):

To start, the result of the $305.56 comes from the addition of two amounts divided by 36:

$ 9,800.00 + $ 1,200.00 / 36 = $305.56 (which shows $305.555555 in the background when I click on the $305.56)

This same amount of $305.56 ($305.55555) is automatically input in another field but shows as $305.555556 in the background - this time with a 6 at the end).

So now, when the $305.56 (from the field showing as $305.555556) is multiplied by 36, the result is:

$ 305.56 * 36 = $11,000.00 (which shows $11,000.0000 when it should actually be $11,000.16


I hope the above makes sense. It seems that the problem stars with the first equation where the addition of the two amounts divided by 36. There are too many decimals in the background and if the number of decimals could be limited to two decimals, perhaps it would resolve the problem.

What do I have to do to limit the number of decimals to two in the first calculation where the division is made?

Thank you again for your help.

Bruno
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Let's look at the math:

You want the following calculation performed:

((9,800 + 1,200.00) / 36) * 36

By each step:

9,800.00 + 1,200.00 = 11,000.00

11,000.00 / 36 = 305.55555555555555555555555555556

305.55555555555555555555555555556 * 36 = 11,000.00

Note the Acrobat JavaScirpt does not round intermediate results nor the inputted data in calculations. So if one enters 305.55555555555555555555555555556 it will display 305.56 for a field formatted as a number with 2 decimal places, but JavaScript will use the entered value or computed value of 305.55555555555555555555555555556 in the calculation.

Apparently you need the intermediate results of the division rounded so the value that is displayed is used in the calculation.

This can be accomplished with using the 'util.prihtf(cString, fValue)' method in Acrobat JavaScirpt or using JavaScript's 'Math.round(fNumber)' and 'Math.pow(fBase, fExp)' methods.

Using the util.printf method:
var fValue = 305.55555555556; // number to be roundedvar fRound = 2; // number of decimal places to round tofValue = util.printf('%,1 .' + fRound + 'f', fValue);

Using JavaScript's Math.round and Math.pow:
var fValue = 305.55555555556; // number to be roundedvar fRound = 2; // number of decimal places to round toMath.round(fValue * Math.pow(10, fRound) ) / Math.pow(10, fRound);

One can create a document level function that can be reused in any number of calculations:
function Round(fValue, fRound){// round fValue to fRound decimal placesreturn  Math.round(fValue * Math.pow(10, fRound) ) / Math.pow(10, fRound);} // end round function

And can use this function in a script:
var fValue = 305.55555555556; // number to be roundedvar fRound = 2; // number of decimal places to round tofValue = Round(fValue, fRound);

George Kaiser

Brunoboy
Registered: Nov 28 2008
Posts: 27
Dear gkaiseril,

Thank you very much for the various codes.

If I use the util.printf method you are providing, do I have to change the figure 305.55555555556 wihtin the code to the name of the field?

This figure will change each time the form will be filled-in by another person.

I am not an expert as you are so where do I place the util.prinf code? Do I place this code in the "Run Custom Validation Script"?


Thank you again in advance,

Burno
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
I would use it for in the "Custom calculation script" for the Amt3D field so the value in that field is what is displayed and the rounded value of the calculation will always be used within the form.

// get lease termvar fLeaseTerm = this.getField('LeaseTerm').value// check to make sure lease term is not blank or zeroif(fLeaseTerm != 0) {compute the valuevar fAmount1 = this.getField('Amount1_FieldName').value;var fAmount2 = this.getField('Amount2_FieldName').value;// unrounded resultvar fResult = (fAmount1 + fAmount2) / fLeaseTerm;// round the result to 2 decimal placesvar fRound = 2;event.value = util.printf('%,1. ' + fRound + 'f', fResult);

George Kaiser

Brunoboy
Registered: Nov 28 2008
Posts: 27
Dear gkaiseril,

I want to take the time to thank you so very much for your patience and your assistance regarding this issue. You have been wonderful and quite the gentleman.

At the end, since nothing seemed to be working out, I decided to use the services of pdfscripting.com in order to solve the problem and also to deal with other pertinent issues also caused because of the multiplication rounding up problem.

Again, thank you very much and have a wonderful day.

Bruno