parseInt and Radix

By  on  

Everyone knows that the parseInt function within JavaScript turns your decimal number or string into a rounded integer.  parseInt turns 10.937 into 10, 0.2 into 0, and "someValue" into NaN.  If you use parseInt without a radix, however, you'll receive a warning that no radix has been provided.  In most cases, the radix you want to use is 10:

parseInt(10.83, 10); // 10, no warning
parseInt(.83, 10); // 0, no warning

parseInt(0.8); // 8, unintended result
parseInt(0.8, 10); // 0, intended result

Using a 10 radix means the number is parsed with a base 10 and thus turns the number into the integer you're expecting, without the annoying warning.  The radix is important if you're need to guarantee accuracy with variable input (basic number, binary, etc.).  For best results, always use a radix of 10!

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

  • By
    dwClickable:  Entire Block Clickable Using MooTools 1.2

    I recently received an email from a reader who was really impressed with Block Clickable, a jQuery script that took the link within a list item and made the entire list item clickable. I thought it was a neat script so I...

Discussion

  1. Nice!!!@

  2. whoever

    It’s especially important when you convert strings like ‘010’ for instance :).

  3. MaxArt

    It may be that I rarely use parseInt, but where’s this warning you’re talking about?
    I can’t see it in the console.

  4. xadet

    @MaxArt jshint and jslint display the warnings, not the console.

    • MaxArt

      Aahh, JSLint, ok.
      Maybe that’s because it’s an instrument of the Devil.

  5. “Everyone knows that the parseInt function within JavaScript turns your decimal number into a rounded integer”
    In reality parseInt turns your strings into rounded integers. I think decimals are cast to strings and then integers when calling this function. To turn decimals into integers there are Math.round / Math.ceil / Math.floor

    Also, parseInt(0.8) returns 0 in the latest Chrome and Firefox.

    All these quick beginners code tips you have been posting lately… is that because of lack of sleep / time due to fatherhood?

  6. parseInt will turn “someValue” to NaN, not 0

  7. Om Shankar

    Nice catch.
    However, I didn’t expect it on your site, as this is a very basic thing put in “Number” sections of many JS Primer Books

  8. A minor point: 10.937 to 10 is not rounding. Perhaps you meant “whole number” not “rounded number”?

  9. 新設する原案もまとめた プラダ 財布 公式。

  10. e cigarette… e cigarette, electronic cigarette review, best electronic cigarette e cig! e cig! electronic cigarette! e cigarettes ecig! e cigarette electronic cigarettes… electronic cigarette reviews? e cigarette electronic cigarette reviews best electronic cigarette. electronic cigarette reviews electronic cigarette… electronic cigarettes best electronic cigarette,

  11. Peter Ho

    I tried on IE10, firefox and chrome, I don’t see the problem. parseInt(“0.8”) returns 0.

  12. Peter : jsLint say “Missing radix parameter” if you don’t set it

  13. The radix is really annoying. If I know the string it an int (from the GUI, say), I just use parseFloat(str).

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!