【问题标题】:Math.round MidPointRounding.AwayFromZero in javascriptjavascript 中的 Math.round MidPointRounding.AwayFromZero
【发布时间】:2015-02-23 21:17:55
【问题描述】:

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

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

我试过了,但没有成功:

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

【问题讨论】:

标签: javascript c# math rounding


【解决方案1】:

如果您知道您将通过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 将它们移回小数点后。

【讨论】:

  • 这救了我的命,坦克了很多 :) 第一块得到了价格 :) 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多