user12821929
user12821929

Reputation:

How to round a whole number in javascript

How to round an integer in javascript to its previous decimal.

Ex.:
15 to 10.
16 to 10.
21 to 20.
29 to 20.

Upvotes: 0

Views: 71

Answers (3)

Prabhat Gupta
Prabhat Gupta

Reputation: 137

you can create your own function to do that

function intFloor(num){
  let temp = num%10;
  return num-temp;
}
console.log(intFloor(15));

if you know prototype You can add this to prototype

Number.prototype.intFloor = function(){
  let temp = this%10;
  return this - temp;
}
console.log((52).intFloor()) // result 50

Upvotes: 0

Dhaval Jardosh
Dhaval Jardosh

Reputation: 7299

Can even use Math.floor()

var num = 24;
var round = Math.floor(num / 10) * 10;
console.log(round)

Upvotes: 2

Prithwee Das
Prithwee Das

Reputation: 5226

This should do the trick

parseInt(15/10) * 10

Upvotes: 2

Related Questions