Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round off the decimal place of a number to the next multiple of 0.05

Tags:

javascript

I want to Round off the decimal place of a number to the next multiple of 5 I have tried this but it's not working for decimal places

const roundOff = (num) =>{

 let temp = parseFloat(num.toFixed(2)) // to make it 2 place decimal
 return Math.ceil(temp / 5) * 5

  }

roundOff(54.5678)  // should give 54.60
roundOff(43.738)   // should give 43.75
roundOff(89.982)   // should give 90.00 
roundOff(80.034)   // should give 80.05
like image 553
geek glance Avatar asked Dec 08 '22 09:12

geek glance


1 Answers

const roundOff = (num) =>{
 return (Math.ceil(num * 20) / 20).toFixed(2)
}

console.log(roundOff(54.5678))  // should give 54.60
console.log(roundOff(43.738))   // should give 43.75
console.log(roundOff(89.982))   // should give 90.00 
console.log(roundOff(80.034)) 
like image 101
Vulwsztyn Avatar answered Dec 09 '22 22:12

Vulwsztyn