Home > Back-end >  Why does NodeJS treat a number as an integer?
Why does NodeJS treat a number as an integer?

Time:03-19

I am subtracting two very big numbers from each other. I then try to divide the result by 1000.0, to convert from millisec to seconds. For reasons beyond me, I am losing the decimals. Can someone explain me why?

let intervalMs = (interval.toMs - interval.fromMs) // Two big numbers
console.log(intervalMs) // Prints "43200000"
let intervalSec = intervalMs / 1000.0
console.log(intervalSec) // Prints "43200" ?!?!

I also tried "casting" the intervalMs to a number:

let intervalMs = Number(interval.toMs - interval.fromMs)

But the results are the same

CodePudding user response:

That's becase decimals equal 0, try to divide by 3003 you will get decimals.

To get 0 decimals use:

console.log(intervalSec.toFixed(10));
  • Related