Skip to main content
Participant
Oct 21, 2016 7:35 AM UTC
Answered

How to a get the whole number without rounding in JavaScript

Hi, I'm a novice at JavaScript so please excuse me if my question is a bit simple.

In a PDF form, using Acrobat 9 Pro I'm trying to preform some mathematical calculations in a custom calculation script and I've hit a brick wall

I have a number in a variable, lets say 3.7. I want to return the whole number component, the "3" without rounding it up or down. I know I've done this in the past in AccessBasic, but I'm at a loss as to how to achieve it in JavaScript

I tried using toFixed(0) but that still round up. I've searched various JavaScript references but I'm unable to find a function that would help me.

The reason I'm trying to do it is rather convoluted, so I'm trying not to confuse you all by entering a long winded description of the problem. Any pointers would be gratefully received.

This topic is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
Oct 21, 2016 10:30 AM UTC

Try it in the JavaScript debugger.

3.2 - 0.5 is 2.7, this will be rounded to 3

10 replies

Bernd Alheit
Community Expert
Community Expert
Oct 21, 2016 10:07 AM UTC

Use something like this:

var a = 3.7;

Math.round(a - 0.5);

Translate
Participant
Oct 21, 2016 10:15 AM UTC

Bernd,

Thank you for responding. Based on your answer, what happens is the value is say 3.2? That would then produce a result of 2 which is not what I'm after.

For example what I want is

40.6 Result = 40

40.1 Result = 40

40.9 Result = 40.

I just want to trim the number to it's whole value without rounding up or down.

Translate
Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
Oct 21, 2016 10:30 AM UTC

Try it in the JavaScript debugger.

3.2 - 0.5 is 2.7, this will be rounded to 3

Translate