Javascript 如何根据7中提供的小数点对数字进行四舍五入

Javascript 如何根据7中提供的小数点对数字进行四舍五入,javascript,angular,math,rounding,Javascript,Angular,Math,Rounding,如何根据上面给出的小数点对数字进行四舍五入。根据输入的数字和小数点,我希望将数字四舍五入,如上所述 Input Number : 3266.528 deciaml Point: 1 output for decimal point (1) : 3266.5 output for decimal point (2) : 3266.53 output for decimal point (3) : 3266.528 output for decimal point (4) : 3266.5280

如何根据上面给出的小数点对数字进行四舍五入。根据输入的数字和小数点,我希望将数字四舍五入,如上所述

Input Number : 3266.528
deciaml Point: 1
output for decimal point (1) : 3266.5
output for decimal point (2) : 3266.53
output for decimal point (3) : 3266.528
output for decimal point (4) : 3266.5280
其中100将四舍五入到小数点后2位,1000将四舍五入到小数点后3位,以此类推。。如果您愿意,您可以参数化并使用
Math.pow(10,idx)
,例如

const num = 3266.528;
const result = Math.round(num * 100) / 100;
对数字使用.toFixed()。您给出的参数将决定小数点后要返回的位数

const round = (n, dec) => (Math.round(n * Math.pow(10, dec)) / Math.pow(10, dec));

看看十进制管道:不完全是-console.log(num.toFixed(2))->
3266.54
但这是将小数四舍五入到x的正确方法。
let num = 3266.5390
console.log(num.toFixed(2)) // answer will be 3266.54
console.log(num.toFixed(3)) // answer will be 3266.539