How to round up decimal points of a number in Node.js

1 Answer

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


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

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


    
    
    
/*
    
run:
    
199.2
199.18
199.178
200
199.98
199.978
    
*/

 



answered Jun 8, 2022 by avibootz

Related questions

1 answer 135 views
1 answer 158 views
1 answer 122 views
122 views asked Jun 8, 2022 by avibootz
1 answer 136 views
1 answer 113 views
1 answer 114 views
1 answer 124 views
...