7 Replies Latest reply on Jan 27, 2017 1:04 PM by Karl Heinz Kremer

# How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

This is the script I am working with, what would it need to look like to achieve this?

var VOL = this.getField("VOL").value;

var CFM50 = this.getField("CFM50").value

if (VOL == "" || CFM50 == "")

[event.value = 0]

else event.value = (CFM50*60/VOL)

Thank you guys (and girls) in advance, I am very new to javascript and adobe, this is actually my first day working with it and Ive been struggling to make this work.

• ###### 1. Re: How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

For always rounding up to the next integer, you would use the Math.ceil() method.

Your code may work, but I strongly suggest to stay with complete syntax, as it makes your code more readable and understandable. It would then look like this:

var VOL = this.getField("VOL").value ;

var CFM50 = this.getField("CFM50").value ;

if (VOL == "" || VOL == 0 || CFM50 == "") {

event.value = 0 ;

} else {

event.value = Math.ceil(CFM50 * 60 / VOL) ;

}

Note that I also test for VOL to be 0, because that would cause a division by zero, which would lead to infinity as event.value.

You may also take into consideration whether event.value = 0 is a legal value if one of the input values is not (yet) defined.

Hope this can help.

• ###### 2. Re: How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

Thank you for the quick response! I plugged in your code and it seems to have worked somewhat, but still isnt doing exactly what I need. The result is now rounding up and staying at a 10th decimal place, but it is rounding out to a whole number and I need it to be to a tenth and show that way. When I plug in 20123 for volume, and 1688 for CFM50 the result is showing up at 6.0. The actual result is 5.033... Is it possible to have this show as 5.1? It seems like the more I play around with this the more difficult it is.

• ###### 3. Re: How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

In order to round to a certain decimal level, you have to first multiply the number by e.g. 10, and then round, and finally divide by e.g. 10 again (or 100 or 1000, depending on how many decimals you need):

```var n = 100/6;
var n_rounded = Math.ceil(n*10)/10;
```
• ###### 4. Re: How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

Thanks Karl, I'm one step closer to getting this form to function as it needs to! I apologize in advance for asking you to spell it out completely, but like I said before I have essentially no experience with JavaScript.  I work in energy efficiency and this form helps with inspections to ensure homes are meet various effieicney standards.

So the complete script would look like this?

var VOL = this.getField("VOL").value ;

var CFM50 = this.getField("CFM50").value ;

if (VOL == "" || VOL == 0 || CFM50 == "") {

event.value = 0 ;

} else {

event.value = Math.ceil(CFM50 * 60 / VOL)*10/10 ;

}

Thank you for your patience. I plan on getting to know the basics and becoming more fluent with JavaScript as I move forward in my career, I can tell it is a VERY useful skill to have!

• ###### 5. Re: How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

You cannot program in JavaScript without a basic understanding of the language. Take a look here for some guidance about how to learn about JavaScript: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

You are almost there: Chances are you got exactly the same output as before. The trick is to move the "*10" part into the parenthesis so that it gets executed before the rounding occurs. With every JavaScript expression, there is an order in which the different operations are performed (just like in mathematics, when you group things with parenthesis and take the operator precedence between */ and +- into account).

What we want is the following:

- take the result of a calculation

- multiply that by 10

- round

- divide the result by 10

You would need this:

```var VOL = this.getField("VOL").value;
var CFM50 = this.getField("CFM50").value;
if (VOL == "" || VOL == 0 || CFM50 == "") {
event.value = 0;
} else {
event.value = Math.ceil(CFM50 * 60 * 10 / VOL) / 10;
}
```
• ###### 6. Re: How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

Thank you soooooo much, this community is great. As I learn more I hope to be able to contribute too. Have an awesome weekend guys!

• ###### 7. Re: How can I have a calculation always round up to the nearest 10th when the number is any amount above the whole number? For example 4.01 -> 4.1 instead of going down to 4.0

Glad that I could help. We are always looking for others to help share the load

Don't forget to mark comments as "Correct Answer" or "Helpful". This makes sure that others with similar problems with have a way to quickly find what works.