Skip to main content
Participant
Jun 22, 2018 12:17 PM UTC
Question

Round down to 0.5

I can't get the JavaScript to round down to 0.5. In Excel, FLOOR.MATH(A1,0.5) does what I want, but it doesn't do so in Adobe.  For example, I need 1.1 to 1.4 to round down to 1.0, and 1.6 to 1.9 to round down to 1.5.  I have the formula event.value=Math.floor(this.getField("CPE1").value,.5) in Adobe, but this just returns 1.1 to 1.9 to 1.0.  Any ideas?

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

try67
Community Expert
Community Expert
Jun 22, 2018 1:05 PM UTC

You can't just invent parameters and hope for them to work... You need to consult the documentation of the methods you're using.
In this case, there's no built-in command that does it for you in the core syntax, but it's pretty simple to write a function that will do it, like this:

function floorHalf(num) {

    return Math.floor(num*2)/2;

}

You would then call it like this, for example:

floorHalf(3.81)

and it will return 3.5 .

Translate
domgags2Author
Participant
Jun 22, 2018 4:35 PM UTC

Thanks for the reply. Sorry if it came across that I was inventing parameters and hoping they would work. The Floor.Math function I posted did exactly what I wanted it to do in Excel, and in my ignorance, I was hoping that it would work the same way in JavaScript.  I simply don't have the experience in JavaScript to know that it wouldn't work the same way. I'm not sure I understand your answer. Do you mean that I'd have to list every possible number in order to obtain the value I want?  Would I have to do separate lines for 1.1, 1.2, 1.3, 1.4, 1.5, 1.6.1.7, etc.?

Right now, what I have is event.value=FLOOR(this.getField("CPE1").value,.5).

The CPE1 field is what needs to be rounded.  In Excel, the .5 after the comma makes it round down to the nearest .5.  I'm sorry that I don't understand

Translate
try67
Community Expert
Community Expert
Jun 22, 2018 5:38 PM UTC

No, of course not. You just need to place the function itself as a doc-level script and then change your code to:

event.value = floorHalf(Number(this.getField("CPE1").value));

Translate