Questions tagged [parseint]

parseInt is a function that returns the integer representation of a given string.

parseInt is a function that returns the integer representation of a given string. It requires one parameter, but usually it can accept two parameters:

parseInt(toParse)
parseInt(toParse, radix)

Where toParse is a string, and radix is an integer. When the one-parameter function is called, a radix of 10 is used for the conversion.

In Javascript the syntax is parseInt(toParse, radix) Examples of use with :

document.write(parseInt("10") + "<br>");
document.write(parseInt("82", 16) + "<br>");
document.write(parseInt("11.25") + "<br>");
document.write(parseInt("23 11 55 32") + "<br>");
document.write(parseInt(" 7 ") + "<br>");
document.write(parseInt("45 programmers") + "<br>");
document.write(parseInt("I am 23") + "<br>");

Output:

10
130
11
23
7
45
NaN

In Java the syntax is Integer.parseInt(toParse, radix). Unlike javascript, with java you have to be sure that you try to parse an integer. Parsing 11.25, 23 11 55 32 or 45 programmers will fire a NumberFormatException.

Examples:

System.out.println(Integer.parseInt("10"));
System.out.println(Integer.parseInt("82", 16));
System.out.println(Integer.parseInt("11.25"));
System.out.println(Integer.parseInt("23 11 55 32"));
System.out.println(Integer.parseInt(" 7 "));
System.out.println(Integer.parseInt("45 programmers"));
System.out.println(Integer.parseInt("I am 23"));

Output:

10
130
Exception in thread "main" java.lang.NumberFormatException: For input string: "11.25"
774 questions
25
votes
5 answers

Javascript parsing int64

How can I convert a long integer (as a string) to a numerical format in Javascript without javascript rounding it? var ThisInt = '9223372036854775808' alert(ThisInt+'\r' +parseFloat(ThisInt).toString()+'\r' +parseInt(ThisInt).toString()); I need to…
Oppdal
  • 591
  • 3
  • 7
  • 15
20
votes
7 answers

Proper way to avoid parseInt throwing a NumberFormatException for input string: ""

When I run parseInt: Integer.parseInt(myString); it throws: NumberFormatException: For input string: "" Does this mean I have do something like this? if(StringUtils.isNotBlank(myString)) return Integer.parseInt(myString); else return 0;
Mocktagish
  • 291
  • 1
  • 4
  • 8
20
votes
3 answers

Java: Comparing ints and Strings - Performance

I have a String and an int, lets say: String str = "12345"; and int num = 12345;. What is the fastest way of seeing if they are the same, str.equals("" + num) or num == Integer.parseInt(str) (Or is there a faster way?)? This is the source code for…
Justin
  • 24,288
  • 12
  • 92
  • 142
15
votes
1 answer

why parseInt('08') is giving 0, whereas parseInt('07') is giving 7

Possible Duplicate: Workarounds for JavaScript parseInt octal bug I am working on javascript, and I seem to find this strange, that the javascript function parseInt('08') is returning 0 and parseInt('07') is returning 7. this behavior seems to be…
Brij
  • 11,731
  • 22
  • 78
  • 116
14
votes
4 answers

Converting 32-bit binary string with Integer.parseInt fails

Why does this part of code fail: Integer.parseInt("11000000000000000000000000000000",2); Exception in thread "main" java.lang.NumberFormatException: For input string: "11000000000000000000000000000000" As far as I understand Integer is a 32 bit…
Aliens
  • 984
  • 3
  • 14
  • 23
14
votes
8 answers

How can I parse String to int with the default value?

I need to parse a string user id into integer for that I used Integer.parseInt(String s) but it returns null/nil, if there string has/holds non-decimal/integer value and in that case, I need to assign default integer value as 0. I tried this but it…
Krunal
  • 77,632
  • 48
  • 245
  • 261
14
votes
1 answer

Why am I getting weird result using parseInt in node.js? (different result from chrome js console)

I just noticed that: //IN CHROME JS CONSOLE parseInt("03010123"); //prints: 3010123 //IN NODE.JS parseInt("03010123"); //prints: 790611 Since both are based on V8, why same operation yielding different results???
Renato Gama
  • 16,431
  • 12
  • 58
  • 92
14
votes
4 answers

When to use parseInt

Which rule do I have to follow when extracting numbers out of DOM and calcluation with them? How does javascript knows that a value is a number or not? Should I always use parseInt? Given following Code: HTML
5
iappwebdev
  • 5,880
  • 1
  • 30
  • 47
13
votes
2 answers

JsPerf: ParseInt vs Plus conversion

I've try to probe that plus (+) conversion is faster than parseInt with the following jsperf, and the results surprised me: Parse vs Plus Preparation code Parse…
Sebastian Pederiva
  • 381
  • 2
  • 4
  • 16
12
votes
7 answers

parseInt() parses number literals with exponent incorrectly

I have just observed that the parseInt function doesn't take care about the decimals in case of integers (numbers containing the e character). Let's take an example: -3.67394039744206e-15 > parseInt(-3.67394039744206e-15) -3 >…
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
11
votes
7 answers

Is there an equivalent to JavaScript parseInt in C#?

I was wondering if anyone had put together something or had seen something equivalent to the JavaScript parseInt for C#. Specifically, i'm looking to take a string like: 123abc4567890 and return only the first valid integer 123 I have a static…
Doozer Blake
  • 7,677
  • 2
  • 29
  • 40
11
votes
7 answers

Beginner Java Question about Integer.parseInt() and casting

so when casting like in the statement below :- int randomNumber=(int) (Math.random()*5) it causes the random no. generated to get converted into an int.. Also there's this method I just came across Integer.parseInt() which does the same ! i.e…
Serenity
  • 4,968
  • 19
  • 65
  • 104
11
votes
4 answers

parseInt always returns NaN?

long story short, i was trying to validate a phone field. ive added the isNaN and parseInt for checking the " " in the field but that said This below never validates to true..what am i missing? if(isNaN(parseInt(phone))){ error.text("Sorry…
somdow
  • 6,268
  • 10
  • 40
  • 58
10
votes
4 answers

javascript parseInt to remove spaces from a string

I have an example of data that has spaces between the numbers, however I want to return the whole number without the spaces: mynumber = parseInt("120 000", 10); console.log(mynumber); // 120 i want it to return 120000. Could somebody help me with…
passion
  • 1,000
  • 6
  • 20
  • 47
10
votes
5 answers

Why does 230/100*100 not return 230?

Possible Duplicate: Is JavaScript’s Math broken? In Javascript, I cannot figure out why 230/100*100 returns 229.99999999999997, while 240/100*100 returns 240. This also applies to 460, 920 and so on... Is there any solution?
Vicentiu B
  • 141
  • 1
  • 8
1
2
3
51 52