Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❀ đŸ’»
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

JavaScript - how to convert string to integer number?

0 contributions
3 points

In this post we can find different ways of how to parse integer number with parseInt function in JavaScript.

Integer parsing example

This section shows how to convert with built-in parseInt method some text to integer.

// ONLINE-RUNNER:browser;

var text = '123';
var number = parseInt(text);

console.log(number === 123); // true

Using parseInt() method we can also parse floating point number and it will be rounded to integer, look at below example.

// ONLINE-RUNNER:browser;

var text = '123.12';
var number = parseInt(text);

console.log(text);
console.log(number);
console.log(number === 123); // true

Parsing number with unit examples

parseInt method during conversion ignores all characters that are not digits and occur after number. It makes possible to parse numbers with units.

// ONLINE-RUNNER:browser;

var number = parseInt('1px'); // pixels (px)
console.log(number === 1); // true

var number = parseInt('2cm'); // centimeters (cm)
console.log(number === 2); // true

var number = parseInt('3in'); // inches (in)
console.log(number === 3); // true

var number = parseInt('4%'); // percentages (%)
console.log(number === 4); // true

See also

  1. JavaScript - how to convert string to number?
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more