Skip to main content
Participating Frequently
January 20, 2019
Question

Custom Calculation with rounded figure

  • January 20, 2019
  • 3 replies
  • 1194 views

I do the following calculation on a local tax form for Total Tax Liability:

event.value = this.getField("TotalTaxEINP").value * this.getField("Percent").value / 100.0;

This value is rounded up, and then either Total Tax Liability (TotalTaxLiab) is subtracted from Total Payments and Credits (TotalPayCr) as follows:

var v1 = Number(this.getField("TotalPayCr").valueAsString);

var v2 = Number(this.getField("TotalTaxLiab").valueAsString);

event.value = Math.max(0, v1-v2);

OR – Total Payments and Credits (TotalPayCr) is subtracted from Total Tax Liability (TotalTaxLiab) as follows:

var v1 = Number(this.getField("TotalTaxLiab").valueAsString);

var v2 = Number(this.getField("TotalPayCr").valueAsString);

event.value = Math.max(0, v1-v2);

My PROBLEM is that when the first calculation is rounded up – it shows the rounded up number on the form – but it is subtracting the number BEFORE rounding in the next 2 calculations (sometimes making me off by $1.00)

How can I get the second 2 calculations to use the rounded up figure?

This topic has been closed for replies.

3 replies

Inspiring
January 20, 2019

Formatting a field to a specific number format does not change the value of the field, it only changes the displayed value of the field. You need to force or set the value of the field to the rounded value. One can use a the util.printf() method or write a custom function. One needs to be careful when using the util.printf() method since it returns a string and not a number type variable. When computing tax values one needs to understand how the taxing authority wants the calculation performed. I would also round all input values to the tax calculation at the point they are calculated.

Your calculation script could be:

var v1 = this.getField("TotalTaxLiab").value;

var v2 = this.getField("TotalPayCr").value;

var taxLiability = util.printf("%,1 3.0f", v1) - util.printf("%,1 3.0f", v2);

event.value = Math.max(0, taxLiability);

try67
Community Expert
Community Expert
January 20, 2019

The "rounding up" you're seeing is probably caused by the field's Format setting, and is only a visual thing, not an actual change in the value.

You should instead do it directly in the code, like this:

var v1 = Number(this.getField("TotalPayCr").valueAsString);

var v2 = Number(this.getField("TotalTaxLiab").valueAsString);

event.value = Math.max(0, Math.ceil(v1-v2));

jyoho107Author
Participating Frequently
January 21, 2019

Thank you very much for your assistance

The only field that is NOT rounding up – is the one that computes the percent – I still get (for example) $379.50 for a product.

This is my script:

var v1 = Number(this.getField("TotalTaxEINP").valueAsString);

var v2 = Number(this.getField("Percent").valueAsString);

event.value = Math.ceil(v1 * v2) / 100.0;

Bernd Alheit
Community Expert
Community Expert
January 20, 2019

You must round the values. I see nothing about this in your code.

jyoho107Author
Participating Frequently
January 20, 2019

You’re right – I am just using formatting – I’m a beginner – and thank you for your attention to my issue.