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

Need help with stopping calculation field from rounding

New Here ,
Nov 15, 2021 Nov 15, 2021

Hi,

 

I have a simple calculation field, which is averaging 5 fields together, and i have set the field to a number format with 2 decimal places.  Now I realize that if you click on the number, it shows you the exact number, but I would like to have it display the real value to the 2nd decimal, without adding another decimal (which is the easiest way)

 

I have tried a custom calculation code with the math.round, math.ceil and (num).fixto(2) which I found from a few articles, but nothing is helping, so I was wondering if anyone had any other suggestions to try?

TOPICS
JavaScript , PDF forms
2.2K
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
1 ACCEPTED SOLUTION
Community Expert ,
Nov 16, 2021 Nov 16, 2021

Try this:

event.value = (this.getField("Overall Score").value - 0.005).toFixed(2);

View solution in original post

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 ,
Nov 15, 2021 Nov 15, 2021

Remove the Format option and round the value in your calculation script, using toFixed(2), not fixto(2), which doesn't exist.

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 ,
Nov 15, 2021 Nov 15, 2021

sorry that's what i meant to put toFixed(2), but I will try that removing formatting

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 ,
Nov 16, 2021 Nov 16, 2021

hi there so I tried that, and it is rounding the decimal still.

so I have a field with the following custom calculation code:

// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
for (i = 1; i <= 13; i++) {
  v = +getField("A" + i).value;
  if (v !== 0) {
  // increment the non-blank/zero field counter
  num++;
  // add the field value to the running total
  sum += v;
  }
}
// Calculate the average
if (num) {
  event.value = sum / num;
} else {
  // All fields are empty, so set to blank
  event.value = "";
}

and set formatting to "none" so for this example the calculation came out to be: 2.7384615

I then created a field with this custom calculation:

event.value = this.getField("Overall Score").value.toFixed(2)

Formatting is set to "none" as well, and when I looked at the value it showed as: 2.74, but when I changed the code to ".toFixed(3)" it displayed the value as 2.738

I am not sure what is going on, or why its still rounding when set to 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
Community Expert ,
Nov 16, 2021 Nov 16, 2021

I don't understand what you mean. It rounded to 2 decimals when you specified 2 as the parameter and to 3 when you specified 3. What's wrong with that?

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 ,
Nov 16, 2021 Nov 16, 2021

Try this:

event.value = (this.getField("Overall Score").value - 0.005).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 ,
Nov 16, 2021 Nov 16, 2021

I have tried that code and no it doesn't work, the calculation is off, for instance the real number is: 3.01538461, but the field with the script you sent displays this value: 3.03

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 ,
Nov 16, 2021 Nov 16, 2021

Can you share the file in question, please?

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 ,
Nov 16, 2021 Nov 16, 2021

> for instance the real number is: 3.01538461, but the field with the script you sent displays this value: 3.03

 

This is not what I'm getting. See:

 

try67_0-1637081492529.png

 

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 ,
Nov 16, 2021 Nov 16, 2021

here is the form

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 ,
Nov 16, 2021 Nov 16, 2021

So there are two sections that I am using the code you provided, and in the second section that has a dropdown of 1 to 5, with 16 values, using the simple average calculation, your script works perfectly the 10 times I have tested it out, just not with the script that only average's the first section, which has a max of 13 scores, but only calculates and averages the ones with values.

Also, to just let you know when I reset the form to all zero's, the overall score field with the above code displays a "-0.01" by default.

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 ,
Nov 16, 2021 Nov 16, 2021

Add this:

if (event.value <= 0) event.value = "";

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 ,
Nov 16, 2021 Nov 16, 2021

I added the code, and that does help with keeping it '0" by default, but as the picture shows, if I don't have values in all 3 calculations, the code you gave me isn't calculating correctly.

score.png

 

actual score on the right.

How can I fix this?

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 ,
Nov 16, 2021 Nov 16, 2021

correction to above:

13 not 3

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 ,
Nov 16, 2021 Nov 16, 2021
LATEST

So i was able to figure this out on my own. 

What i did to fix the decimal issue, so rounding didn't occur at 2 decimal places was add both of @Bernd Alheit code pieces to the end of my custom calculation for averaging only values, it worked how I wanted it to.

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