【问题标题】:How to add Math.round to .map [duplicate]如何将 Math.round 添​​加到 .map [重复]
【发布时间】:2022-01-13 23:00:01
【问题描述】:

data.map(item => item.Points = item.Points / item.Games);

所以我从 api 中提取这些数据,然后我将这两个数字相除,我想将它四舍五入到最接近的第一个小数,因为它的结果是 27.77777777,我希望它只是 27.8

【问题讨论】:

标签: javascript html arrays math.round


【解决方案1】:

这很容易做到。要保留一位小数,您可以使用 Math.round(num * 10) / 10 之类的内容。将此添加到您的地图功能后,您会得到如下内容:

data.map(item => item.Points = Math.round(item.Points / item.Games * 10) / 10) / 10));

以下是使用虚拟数字的示例:

console.log(Math.round(25 / 4 * 10) / 10);

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2020-04-27
    • 1970-01-01
    • 2020-02-28
    • 2018-07-29
    • 2013-11-16
    • 2013-08-10
    • 1970-01-01
    • 2014-08-07
    • 2015-01-14
    相关资源
    最近更新 更多