0

I want to write a function to trim additional decimal 0's from floating point numbers like 2.0, 5.00 but not 3.04.

so I wrote this:

const trimZeroFromDecimal = value =>
  parseInt(value) === value ? parseInt(value) : value;

but will this equality between 2.0 and 2 hold true in all javascript environments - or are there any quirks to it?

Thanks for your help

Probosckie
  • 1,585
  • 4
  • 25
  • 40
  • 5
    There aren't any "extra" zeroes. JavaScript numbers are all floating point numbers and `2`, `2.0`, `2.00`, `2.000`, etc are all the same value. `2.0 === 2` is literally the same as `2 === 2` you've just used more code to express it. – VLAZ Feb 03 '21 at 06:38
  • How are passing the data to `trimZeroFromDecimal`? Does that come from an input? `2.0` is just number literal and it is no different to 2. – adiga Feb 03 '21 at 06:39

0 Answers0