46

I have the following JS immbedded in a page:

var round = Math.round;
var id = $(this).attr("id");

var len = id.length;
var indexPos = len -1; // index of the number so that we can split this up and used it as a title

var pasType = id.substring(0, indexPos); // adult, child or infant
var ind = round(id.substring(indexPos)); // converts the string index to an integer
var number = (id.substring(indexPos) + 1); // creates the number that will go in the title
window.alert(number);

id will be something like adult0, and I need to take that string and split it into adult and 0 - this part works fine.

The problem comes in when I try to increment the 0. As you can see I use Math.round to convert it to an integer, and then add 1 to it - I expect 0 to be 1 after this. However, it doesn't seem to be converting it to integer, because I get 01, not 1. When testing this with adult1 the alert I get is 11.

I'm using this question for reference, and have also tried var number += id.substring(indexPos);, which breaks the JS (unexpected identifier '+=')

Does anyone know what I'm doing wrong? Is there a better way of doing this?

Community
  • 1
  • 1
Skytiger
  • 1,745
  • 4
  • 26
  • 53

7 Answers7

67

The parseInt() function parses a string and returns an integer,10 is the Radix or Base [DOC]

var number = parseInt(id.substring(indexPos) , 10 ) + 1;
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
  • FYI: *always* pass the radix parameter to parseInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt – Factor Mystic Feb 28 '14 at 19:45
13

Convert by Number Class:-

Eg:

var n = Number("103");
console.log(n+1)

Output: 104

Note:- Number is class. When we pass string, then constructor of Number class will convert it.

Arshid KV
  • 9,631
  • 3
  • 35
  • 36
12

This is to do with JavaScript's + in operator - if a number and a string are "added" up, the number is converted into a string:

0 + 1; //1
'0' + 1; // '01'

To solve this, use the + unary operator, or use parseInt():

+'0' + 1; // 1
parseInt('0', 10) + 1; // 1

The unary + operator converts it into a number (however if it's a decimal it will retain the decimal places), and parseInt() is self-explanatory (converts into number, ignoring decimal places).

The second argument is necessary for parseInt() to use the correct base when leading 0s are placed:

parseInt('010'); // 8 in older browsers, 10 in newer browsers
parseInt('010', 10); // always 10 no matter what

There's also parseFloat() if you need to convert decimals in strings to their numeric value - + can do that too but it behaves slightly differently: that's another story though.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
10

JS will think that the 0 is a string, which it actually is, to convert it to a int, use the: parseInt() function, like:

var numberAsInt = parseInt(number, 10);  
// Second arg is radix, 10 is decimal.

If the number is not possible to convert to a int, it will return NaN, so I would recommend a check for that too in code used in production or at least if you are not 100% sure of the input.

Jite
  • 5,761
  • 2
  • 23
  • 37
5

Although parseInt is the official function to do this, you can achieve the same with this code:

number*1

The advantage is that you save some characters, which might save bandwidth if your code has to lots of such conversations.

Alexander Jank
  • 2,440
  • 2
  • 18
  • 19
  • 1
    Note that the behaviour of `number*1` is not the same as `parseInt` for all inputs. – Dan Feb 17 '21 at 21:59
3

Use parseInt():

var number = (parseInt(id.substring(indexPos)) + 1);` // creates the number that will go in the title
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
juju
  • 449
  • 5
  • 16
2

If you are sure id.substring(indexPos) is a number, you can do it like so:

var number = Number(id.substring(indexPos)) + 1;

Otherwise I suggest checking if the Number function evaluates correctly.

user1203684
  • 102
  • 2
  • 6