Skip to main content
Participant
October 21, 2016
Answered

How to a get the whole number without rounding in JavaScript

  • October 21, 2016
  • 1 reply
  • 2613 views

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 has been closed for replies.
Correct answer Bernd Alheit

Try it in the JavaScript debugger.

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

1 reply

Bernd Alheit
Community Expert
Community Expert
October 21, 2016

Use something like this:

var a = 3.7;

Math.round(a - 0.5);

Participant
October 21, 2016

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.

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
October 21, 2016

Try it in the JavaScript debugger.

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