Math.round MidPointRounding.AwayFromZero 在 javascript

Math.round MidPointRounding.AwayFromZero in javascript

我想在 javascript 中从 c# 中的这一行得到相同的结果:

round = Math.Round((17245.22 / 100), 2, MidpointRounding.AwayFromZero);
// Outputs: 172.45

我已经试过了,但没有成功:

var round = Math.round(value/100).toFixed(2);

如果你知道你将跳水100,你可以先取整再除:

var round = Math.round(value)/100; //still equals 172.45

但是,如果您不知道潜水时要带什么,您可以采用这种更通用的形式:

var round = Math.round(value/divisor*100)/100; //will always have exactly 2 decimal points

在这种情况下,*100 将在 Math.round 后保留 2 个小数点,而 /100 移动将它们移回小数点后。