Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest way would be to use the native <code>Number</code> function:</p> <pre><code>var x = Number("1000") </code></pre> <p>If that doesn't work for you, then there are the <strong>parseInt</strong>, <strong>unary plus</strong>, <strong>parseFloat with floor</strong>, and <strong>Math.round</strong> methods.</p> <p>parseInt:</p> <pre><code>var x = parseInt("1000", 10); // you want to use radix 10 // so you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1]) </code></pre> <p>unary plus if your string is already in the form of an integer:</p> <pre><code>var x = +"1000"; </code></pre> <p>if your string is or might be a float and you want an integer:</p> <pre><code>var x = Math.floor("1000.01"); //floor automatically converts string to number </code></pre> <p>or, if you're going to be using Math.floor several times:</p> <pre><code>var floor = Math.floor; var x = floor("1000.01"); </code></pre> <p>If you're the type who forgets to put the radix in when you call parseInt, you can use parseFloat and round it however you like. Here I use floor.</p> <pre><code>var floor = Math.floor; var x = floor(parseFloat("1000.01")); </code></pre> <p>Interestingly, Math.round (like Math.floor) will do a string to number conversion, so if you want the number rounded (or if you have an integer in the string), this is a great way, maybe my favorite:</p> <pre><code>var round = Math.round; var x = round("1000"); //equivalent to round("1000",0) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload