How to round up decimal points of a number in TypeScript

1 Answer

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


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

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


    
    
    
/*
    
run:
    
179.2 
179.18 
179.178 
180 
179.98 
179.978 
    
*/

 



answered Jun 8, 2022 by avibootz

Related questions

1 answer 129 views
1 answer 163 views
1 answer 117 views
1 answer 125 views
1 answer 120 views
...