{"id":978,"date":"2012-03-27T20:24:33","date_gmt":"2012-03-28T02:24:33","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=978"},"modified":"2012-03-27T22:18:25","modified_gmt":"2012-03-28T04:18:25","slug":"javascript-error-parseint-fails-when-supplied-with-large-numbers","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/03\/javascript-error-parseint-fails-when-supplied-with-large-numbers\/","title":{"rendered":"Javascript Error: ParseInt fails when supplied with large numbers"},"content":{"rendered":"<p>ParseInt returns 1 for when passed any number greater or equal to 1e+21.<br \/>\n<code><\/p>\n<pre lang='javascript'>\r\nvar x = 1.0e+2;\r\nconsole.log( x == 1e+2 );\r\nconsole.log( parseInt( x, 10 ) == 100 );\r\nconsole.log( parseFloat( x ) == 100 );\r\n\r\nvar x = 1.0e+21;\r\nconsole.log( x == 1e+21 );\r\nconsole.log( parseInt( x, 10 ) == 1 );\r\nconsole.log( parseFloat( x ) == 1e+21 );\r\n<\/pre>\n<p><\/code><\/p>\n<p>What do you think the problem is?<br \/>\nI think the problem by be because numbers 1e+21 and greater are represented as strings.<br \/>\nParseInt might only be looking at valid numbers to parse the number to into an integer. Since e in &#8216;1e21&#8217; is not a number, 1 is the item returned.<\/p>\n<p>Demo: <a href=\"http:\/\/jsbin.com\/asohaf\/edit#javascript,html\">jsbin.com demo<\/a><\/p>\n<p><b>Update:<\/b><br \/>\n<a href=\"http:\/\/code.google.com\/p\/v8\/issues\/detail?id=1971\">V8 Issue Page<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ParseInt returns 1 for when passed any number greater or equal to 1e+21. var x = 1.0e+2; console.log( x == 1e+2 ); console.log( parseInt( x, 10 ) == 100 ); console.log( parseFloat( x ) == 100 ); var x = 1.0e+21; console.log( x == 1e+21 ); console.log( parseInt( x, 10 ) == 1 ); console.log( &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/03\/javascript-error-parseint-fails-when-supplied-with-large-numbers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript Error: ParseInt fails when supplied with large numbers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[123,164],"class_list":["post-978","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-error","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=978"}],"version-history":[{"count":6,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/978\/revisions"}],"predecessor-version":[{"id":996,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/978\/revisions\/996"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}