-5

I have a list of numbers with 3 decimals. I need to remove the last decimal only if it's zero, e.g.

var x = 1.500 // I need 1.50
var y = 1.490 // I need 1.49
var z = 1.579 // I need 1.579

with

var x = 1.500
var noZeroes = x.toString() // I get "1.5", I need "1.50"
kurtko
  • 1,978
  • 4
  • 30
  • 47

1 Answers1

4

You could check the last digit of a stringed number and remove the last zero, if exists.

console.log([1.5, 1.49, 1.579].map(v => {
    var temp = v.toFixed(3);
    return temp.slice(-1) === '0'
        ? temp.slice(0, -1)
        : temp;
}));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392