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
866
votes
4 answers

Why does parseInt(1/0, 19) return 18?

I have an annoying problem in JavaScript. > parseInt(1 / 0, 19) > 18 Why does the parseInt function return 18?
cebor
  • 6,546
  • 4
  • 24
  • 31
520
votes
11 answers

Difference between parseInt() and valueOf() in Java?

How is parseInt() different from valueOf() ? They appear to do exactly the same thing to me (also goes for parseFloat(), parseDouble(), parseLong() etc, how are they different from Long.valueOf(string) ? Also, which one of these is preferable and…
Ali
  • 261,656
  • 265
  • 575
  • 769
251
votes
10 answers

Java: parse int value from a char

I just want to know if there's a better solution to parse a number from a character in a string (assuming that we know that the character at index n is a number). String element = "el5"; String s; s = ""+element.charAt(2); int x =…
Noya
  • 3,879
  • 3
  • 26
  • 32
229
votes
6 answers

parseInt(null, 24) === 23... wait, what?

Alright, so I was messing around with parseInt to see how it handles values not yet initialized and I stumbled upon this gem. The below happens for any radix 24 or above. parseInt(null, 24) === 23 // evaluates to true I tested it in IE, Chrome and…
Robert
  • 21,110
  • 9
  • 55
  • 65
194
votes
3 answers

Why is it that parseInt(8,3) == NaN and parseInt(16,3) == 1?

I'm reading this but I'm confused by what is written in the parseInt with a radix argument chapter Why is it that parseInt(8, 3) → NaN and parseInt(16, 3) → 1? AFAIK 8 and 16 are not base-3 numbers, so parseInt(16, 3) should return NaN too
Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
189
votes
3 answers

Remove leading zeros from a number in Javascript

Possible Duplicate: Truncate leading zeros of a string in Javascript What is the simplest and cross-browser compatible way to remove leading zeros from a number in Javascript ? e.g. If I have a textbox value as 014 or 065, it should only return…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
72
votes
6 answers

Why is JSON invalid if an integer begins with a leading zero?

I'm importing some JSON files into my Parse.com project, and I keep getting the error "invalid key:value pair". It states that there is an unexpected "8". Here's an example of my JSON: } "Manufacturer":"Manufacturer", "Model":"THIS IS A STRING", …
DJSrA
  • 797
  • 1
  • 5
  • 10
61
votes
2 answers

Why does JavaScript's parseInt(0.0000005) print "5"?

I've read an article about JavaScript parseInt, which had this question: parseInt(0.5); // => 0 parseInt(0.05); // => 0 parseInt(0.005); // => 0 parseInt(0.0005); // => 0 parseInt(0.00005); // => 0 parseInt(0.000005); // =>…
SeyyedKhandon
  • 5,197
  • 8
  • 37
  • 68
55
votes
8 answers

Is NaN equal to NaN?

parseFloat("NaN") returns "NaN", but parseFloat("NaN") == "NaN" returns false. Now, that's probably a good thing that it does return false, but I don't understand how this is so. Did the JavaScript creators just make this a special case? Because…
joseph
  • 767
  • 3
  • 8
  • 16
48
votes
6 answers

Parsing a Hexadecimal String to an Integer throws a NumberFormatException?

So, In Java, you know how you can declare integers like this: int hex = 0x00ff00; I thought that you should be able to reverse that process. I have this code: Integer.valueOf(primary.getFullHex()); where primary is an object of a custom Color…
mattbdean
  • 2,532
  • 5
  • 26
  • 58
41
votes
3 answers

Math.random() returns value greater than one?

While playing around with random numbers in JavaScript I discovered a surprising bug, presumably in the V8 JavaScript engine in Google Chrome. Consider: // Generate a random number [1,5]. var rand5 = function() { return parseInt(Math.random() *…
maerics
  • 151,642
  • 46
  • 269
  • 291
39
votes
8 answers

Java - checking if parseInt throws exception

I'm wondering how to do something only if Integer.parseInt(whatever) doesn't fail. More specifically I have a jTextArea of user specified values seperated by line breaks. I want to check each line to see if can be converted to an int. Figured…
Mike Haye
  • 793
  • 3
  • 12
  • 22
34
votes
3 answers

Using the parseInt() function and the radix parameter with ternary operators

As a case study, I am trying to get to grips with this code snippet that uses the parseInt() function, and have a couple questions: var maxChars = parseInt( formField.attr('maxlength') ? formField.attr('maxlength') : counter.text() ); Why is…
shrewdbeans
  • 11,971
  • 23
  • 69
  • 115
30
votes
6 answers

How to parseInt a string with leading 0

How to parseInt "09" into 9 ?
khelll
  • 23,590
  • 15
  • 91
  • 109
27
votes
4 answers

Why does parseInt('dsff66',16) return 13?

today I stumbled on a strange (in my opinion) case in JavaScript. I passed a non-hexadecimal string to the parseInt function with the base of 16 and...I got the result. I would expect the function to throw some kind of exception or at least return…
Michal Leszczyk
  • 1,849
  • 15
  • 19
1
2 3
51 52