Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Try this:
event.value = (this.getField("Overall Score").value - 0.005).toFixed(2);
Copy link to clipboard
Copied
Remove the Format option and round the value in your calculation script, using toFixed(2), not fixto(2), which doesn't exist.
Copy link to clipboard
Copied
sorry that's what i meant to put toFixed(2), but I will try that removing formatting
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Try this:
event.value = (this.getField("Overall Score").value - 0.005).toFixed(2);
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Can you share the file in question, please?
Copy link to clipboard
Copied
> 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:
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Add this:
if (event.value <= 0) event.value = "";
Copy link to clipboard
Copied
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.
actual score on the right.
How can I fix this?
Copy link to clipboard
Copied
correction to above:
13 not 3
Copy link to clipboard
Copied
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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more