【问题标题】:string number with comma's to integer with comma带逗号的字符串数字到带逗号的整数
【发布时间】:2014-12-18 11:02:47
【问题描述】:

当我将数组中的字符串转换为数组中的整数时,问题是数字是四舍五入的。查看我的代码和 cmets

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] 

这是怎么发生的,我该如何解决这个问题?!

【问题讨论】:

标签: javascript arrays string integer converter


【解决方案1】:

parseInt 给你一个整数,如果你想保留小数,你可以使用

parseFloat(rating[v])

或者:

Number(rating[v])

【讨论】:

  • 谢谢它的工作..不敢相信它是如此简单:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2012-01-09
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多