最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Convert String to Integer in JavaScript - Stack Overflow

programmeradmin3浏览0评论

I have a string value of 41,123 and I want to convert it to an integer in JavaScript.

I tried parseInt(41,123, 10) and parseFloat, but nothing gives me correct answer.

ParseInt, parseFloat work well until ma is encountered, but to above the result is '41'.

Does anyone have an idea about the fix?

I have a string value of 41,123 and I want to convert it to an integer in JavaScript.

I tried parseInt(41,123, 10) and parseFloat, but nothing gives me correct answer.

ParseInt, parseFloat work well until ma is encountered, but to above the result is '41'.

Does anyone have an idea about the fix?

Share Improve this question edited Dec 16, 2014 at 22:19 rgettman 178k30 gold badges279 silver badges361 bronze badges asked Jun 13, 2012 at 8:51 RMNRMN 7529 gold badges25 silver badges46 bronze badges 2
  • parseInt("41,123", 10) = 41, what's wrong with this result? – Fabrizio Calderan Commented Jun 13, 2012 at 8:51
  • Yes, but I think it should be 41123. The ma is a notation for thousands. – beeglebug Commented Jun 13, 2012 at 8:52
Add a ment  | 

3 Answers 3

Reset to default 6
var myInt = parseInt("41,123,10".replace(/,/g,""));

Here is the solution:

Number(s.replace(/,/g, ""))

Regex is needed in replacement to remove all ma characters, otherwise only one ma will be replaced.

You can remove the ma, and then parse; let's say the number is variable s:

parseInt(s.replace(/,/g, '')
发布评论

评论列表(0)

  1. 暂无评论