如何将数组中的所有值舍入为2个小数点 [英] How to round all the values in an array to 2 decimal points

查看:302
本文介绍了如何将数组中的所有值舍入为2个小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数组中的值舍入为2个小数点。我知道我可以使用math.round,但将为整个阵列工作?或者我需要编写一个函数来分别舍入每个值。

解决方案

循环!

  var x = 0; 
var len = my_array.length
while(x my_array [x] = my_array [x] .toFixed(2);
x ++
}

是的,while循环在这里更快。

I am trying to round the values in my array to 2 decimal points. I understand i can use math.round but will that work for an whole array? Or will i need to write a function to round each value individually.

解决方案

Loops!

var x = 0;
var len = my_array.length
while(x < len){ 
    my_array[x] = my_array[x].toFixed(2); 
    x++
}

And, yes, a while loop is faster here.

这篇关于如何将数组中的所有值舍入为2个小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆