Print Page | Close Window

Multiplying?

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=194
Printed Date: 20 Sep 2019 at 9:47pm
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: Multiplying?
Posted By: RaeEveMay
Subject: Multiplying?
Date Posted: 31 Jan 2012 at 12:54am
I've tried this with java too but here is the simple calc. example I am having problems with:

(Value is the product (x) of the following fields:
AmtPay1new, Number of Payments

AmtPay1new value=$916.67
Number of Payments=12

I keep getting $11,000.00 instead of $11,000.04 

Any ideas are greatly appreciated. Is this case 4 cents matter (believe it or not).

Thank you,

Rae





Replies:
Posted By: aandi
Date Posted: 31 Jan 2012 at 4:04pm
If you're working with money exact accuracy is important, so it's vital to understand exactly how JavaScript stores numbers. Auditors will demand as much! A common approach is to work in cents/pennies, and round up only for display.


Posted By: RaeEveMay
Date Posted: 31 Jan 2012 at 6:51pm
My display actually needs to show the cents too (it's for the disclosure of the total of payments on a loan). I am not putting in any rounding options within the .pdf. Is that something that java does all on it's own? If so, how do I get it to knock it off? 

The fields that have a monetary value are formatted as Number, 2 Decimal Places and a separator style of 1,234.56. 

The Number of payments field is formatted as Number, 0 Decimal Places and a separator style of 1234.56.

I'd break it down into cents, like you suggested, but I don't see how it will hold a value if the java continues to round.

Thanks again for any info.

-Rae


Posted By: RaeEveMay
Date Posted: 31 Jan 2012 at 8:50pm
Yeah! I got it. The problem was it was taking the full value of the  AmtPay1new field ( $916.6666666666665) which does, in fact =$11,000.00 so I needed to round that figure in a new field called AMT_PAY1new2points. Here is the script:

var nValue = this.getField("AMT_PAY1new").value;
var nPrecision = +2;
nPrecsision = -nPrecision;
var nAdjustment = Math.pow(10, nPrecision);
nValue = nValue * nAdjustment;
nValue = Math.ceil(nValue);
event.value = nValue / nAdjustment;

My total of payment (and finance charge accordingly) reads $11,000.04



Posted By: aandi
Date Posted: 31 Jan 2012 at 10:34pm
The point about storing dollars and counting cents is that no fractions are stored exactly. They are in floating point, and this can have surprising consequences.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net