Skip to main content
toddalwave
Participant
January 27, 2017
Question

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

  • January 27, 2017
  • 1 reply
  • 1355 views

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.

This topic has been closed for replies.

1 reply

Legend
January 27, 2017

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.

toddalwave
Participant
January 27, 2017

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.

Karl Heinz  Kremer
Community Expert
Community Expert
January 27, 2017

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;