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

How to convert String variable to int in javascript? - Stack Overflow

programmeradmin3浏览0评论

What is the correct way to convert value of String variable to int/numeric variable? Why is bcInt still string and why does isNaN return true?

bc=localStorage.getItem('bc');
var bcInt=parseInt(bc,10);
var bcInt2=1;
console.log("bc------------>" +bc +" isNaN:" +isNaN(bc)); //isNaN returns true
console.log("bcInt------------>" +bcInt +" isNaN:" +isNaN(bcInt)); //isNaN returns true

bcInt2// isNaN returns false

What is the correct way to convert value of String variable to int/numeric variable? Why is bcInt still string and why does isNaN return true?

bc=localStorage.getItem('bc');
var bcInt=parseInt(bc,10);
var bcInt2=1;
console.log("bc------------>" +bc +" isNaN:" +isNaN(bc)); //isNaN returns true
console.log("bcInt------------>" +bcInt +" isNaN:" +isNaN(bcInt)); //isNaN returns true

bcInt2// isNaN returns false
Share Improve this question edited Aug 20, 2012 at 16:04 Wug 13.2k5 gold badges35 silver badges57 bronze badges asked Aug 20, 2012 at 15:38 SamiSami 2,33114 gold badges47 silver badges81 bronze badges 5
  • 1 If parseInt() returns a NaN, then your string doesn't actually contain a numeric representation of a value. If you had told us what the value of "bc" is, then perhaps somebody could help, but you failed to do that. That value is of course the key to the whole problem. – Pointy Commented Aug 20, 2012 at 15:40
  • What is the value contained in the local storage item bc? If it is not a number (empty, got alpha characters etc...), that's what I would expect to see. – Oded Commented Aug 20, 2012 at 15:40
  • 08-20 18:41:02.880: bc------------>"1" isNaN:true – Sami Commented Aug 20, 2012 at 15:45
  • 1 If there are double-quote characters around the 1 then it's not going to work. – Pointy Commented Aug 20, 2012 at 15:47
  • Pointy got a point. I used stringify function in another place and then there was a double-quote and that was the reason! Thanks for helping me everybody. – Sami Commented Aug 20, 2012 at 17:43
Add a ment  | 

3 Answers 3

Reset to default 9

parseInt returns a number only if you pass it a number as first character.

Examples:

parseInt( 'a', 10 ); // NaN
parseInt( 'a10', 10 ); // NaN
parseInt( '10a', 10 ); // 10
parseInt( '', 10 ); // NaN
parseInt( '10', 10 ); // 10

Also, you may take a look at the + operator if you want to get strings that are only numbers.

+'a'; // NaN
+'a10'; // NaN
+'10a'; // NaN
+''; // 0, that's tricky
+'10'; // 10

Edit: According to your ment, I've tested parseInt:

parseInt( '08-20 19:41:02.880', 10 ); // 8

You're doing something else wrong. parseInt returns everything till it's not a number. If the first isn't a number (or it doesn't find any number), it returns NaN.

enter image description here

let no = "25";

typeof no 'string'

let no2 = parseInt(no); no2 25

typeof no2 'number'

The answer is that I used localStorage.setItem('bc',JSON.stringify(bc)) and it added double quote to bc because it was in that case already a string and that's why parseInt wasn't working. Value was ""1"".

发布评论

评论列表(0)

  1. 暂无评论