当我将数组中的字符串转换为数组中的整数时,问题是数字是舍入的。查看我的代码和评论
console.log(rating); // ["6.4", "7.4", "6.3"]
//convert to integer
for (v = 0; v < rating.length; v++) {
rating[v] = (parseInt(rating[v])); //<< here it goes wrong I think..
console.log(rating);
}
rating.sort()
console.log(rating) //expect to see [6.4, 6.3, 7.4]
//what I see [6, 6, 7]
这是怎么发生的,我该如何解决这个问题?!
答案 0 :(得分:1)
parseInt
给你一个整数,如果你想保留小数,你可以使用
parseFloat(rating[v])
或者:
Number(rating[v])