How to round up decimal points of a number in JavaScript

1 Answer

0 votes
function round(num, precision) { 
    precision = Math.pow(10, precision)
    
  	return Math.ceil(num * precision) / precision
}


console.log(round(189.178, 1)); 
console.log(round(189.178, 2)); 
console.log(round(189.178, 3)); 

console.log(round(189.978, 1)); 
console.log(round(189.978, 2)); 
console.log(round(189.978, 3)); 


    
    
    
/*
    
run:
    
189.2
189.18
189.178
190
189.98
189.978
    
*/

 



answered Jun 8, 2022 by avibootz

Related questions

1 answer 132 views
1 answer 146 views
1 answer 275 views
1 answer 138 views
1 answer 163 views
...