Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Add numbers with 3 or more decimal places and round up total

New Here ,
Apr 05, 2017 Apr 05, 2017

I've got several fields that calculate a * b= c.  I'm having a heck of a time trying to add these numbers and round up the total to only 2 decimal points (e.g. 1.625 + 1.3264 = 2.9514) total should be 2.95.  Can anyone help me?

Thanks,

Deb

TOPICS
PDF forms
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2017 Apr 05, 2017

You can use this code as the custom calculation script for "c":

var a = Number(this.getField("a").value);

var b = Number(this.getField("b").value);

event.value = (a+b).toFixed(2);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 05, 2017 Apr 05, 2017

I'm still having trouble.  Here's what I'm trying to do:

115.5*1.15 = 132.825    (the total rounds up and displays 132.83)

195.25*.95 = 185.4875  (the total rounds up and displays 185.49)

The total shows 318.31

I want the displayed numbers in BOLD to total 318.32

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2017 Apr 05, 2017

You need to better define the logic of when it should round up and when down, then...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2017 Apr 05, 2017

Ah, you want each individual number to be rounded as well?

In that case, you can do it like this:

var a = Number(this.getField("a").value).toFixed(2);

var b = Number(this.getField("b").value).toFixed(2);

event.value = (Number(a)+Number(b)).toFixed(2);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 05, 2017 Apr 05, 2017

I really appreciate your help...  My first set of calculations do round up.  However, when it totals, it is using the actual number (132.825) instead of the rounded up number (132.83) etc. so my total still total shows 318.31 instead of 318.32.  I've attached a picture to explain.

Calculation Problem.gif

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2017 Apr 05, 2017

You can use the round function:

Math.round() - JavaScript | MDN

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 05, 2017 Apr 05, 2017
LATEST

There are many ways to accomplish this, some are better than others.

One can use the util.printf method to round the result.

Then there is the toFixed method in JavaScript.

There is the Math.round method in JavaScript, but this is for integers and not floating point numbers, but if one uses the method twice, once to round to a higher level of precision and then to the final precision it should work. This is sometimes referred to as double rounding.

D.P. Story has posted Some Exercises in Rounding on the AcroTex PDF Blog. He includes a document level function to round floating point numbers to a specified value and can round to whole numbers or decimals. Below is the function with a minor change from the original post.

function myRound (n, d)

{

    n = String(n).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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines