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
9
votes
1 answer

How to round off parseFloat results in Jquery/Javascript?

How would I round off a value from a textfield with a parseFloat result in it? This application basically sums up the value of all radio buttons when clicked and displays the sum in a textbox. The code below works perfectly if the radio button value…
Davinchie_214
  • 155
  • 2
  • 2
  • 6
9
votes
4 answers

Crashing due to Integer.parseInt

I'm trying to import text from a text file which has been generated in another Activity. The generated text file is made up of a String ArrayList which only contains numbers and the other random text generated by Android. When I import the text from…
BGM
  • 125
  • 1
  • 1
  • 7
9
votes
6 answers

Using JavaScript's parseInt at end of string

I know that parseInt(myString, 10) // "Never forget the radix" will return a number if the first characters in the string are numerical, but how can I do this in JavaScript if I have a string like "column5" and want to increment it to the next…
sova
  • 5,468
  • 10
  • 40
  • 48
8
votes
3 answers

Strange behaviour with spliting a string in Javascript

I am trying to do something relatively simple. I have a date in this format dd/MM/yyyy eg: var newDate = "‎11‎/‎06‎/‎2015"; And I want to convert it to a date. This code only works in Chrome and Firefox: new Date(newDate) In IE11 I get Nan So I am…
Nick
  • 2,877
  • 2
  • 33
  • 62
7
votes
4 answers

Unable to parse as integer

Alright...I have this .txt file (UTF-8) 4661,SOMETHING,3858884120607,24,24.09 4659,SOMETHING1,3858884120621,24,15.95 4660,SOMETHING2,3858884120614,24,19.58 And this code FileInputStream fis = new FileInputStream(new…
svenkapudija
  • 5,128
  • 14
  • 68
  • 96
7
votes
7 answers

What is the best way to make sure a String can be stored as an int in java

I am making a web-based application and i have text-fields where the values are stored as Strings. The problem is that some of the text-fields are to be parsed into ints and you can store much bigger numbers in Strings than you can in an int. My…
user1423793
  • 279
  • 2
  • 6
  • 16
6
votes
2 answers

Java ParseInt Sanity Check

I'm trying to parse a int from a String array element. Here is my code: String length = messageContents[k].replace("Content-Length:", "").replace(" ", ""); System.out.println("Length is: " + length); int test= Integer.parseInt(length); The…
Tony
  • 3,587
  • 8
  • 44
  • 77
6
votes
2 answers

parseInt % and px problem

parseInt($("div").css("left"), 10); When I try to get value of left, some browsers give result in pixels, some in %. Here is an example http://jsfiddle.net/WTt4s/1/ Google Chrome gives percent value, IE9 gives pixels…
James
  • 42,081
  • 53
  • 136
  • 161
6
votes
4 answers

how to parse Zero (0) as integer in JavaScript

I am working on a basic calculator that takes input like this " 100 + 10/2 + 3 + 0 " and returns the output in a separate field. when I break this thing into an array zero is not parsed as integer. My code is as following var arr = ["100",…
rudra
  • 200
  • 1
  • 12
6
votes
3 answers

javascript, parseInt behavior when passing in a float number

I have the following two parseInt() and I am not quite sure why they gave me different results: alert(parseInt(0.00001)) shows 0; alert(parseInt(0.00000001)) shows 1 My guess is that since parseInt needs string parameter, it treats 0.00001 as…
AlliceSmash
  • 687
  • 1
  • 11
  • 19
6
votes
1 answer

IE8: parseInt('07') vs parseInt('08')

Found out a strange thing in IE8: parseInt('01') //1 parseInt('02') //2 parseInt('03') //3 /*...*/ parseInt('07') //7 parseInt('08') //0 !!! parseIntr('09') //9 ok Can someone clarify?
Michael Zelensky
  • 2,012
  • 3
  • 29
  • 37
5
votes
2 answers

numeric type parse functions exception with negative numbers

System.out.println(Integer.parseInt("7FFFFFFF", 16)); //this is ok. System.out.println(Integer.parseInt("FFFFFFFF", 16)); //this throws Exception System.out.println(Integer.valueOf("FFFFFFFF", 16)); //this throws Exception When I try to convert…
User8500049
  • 410
  • 3
  • 12
5
votes
2 answers

Handling exception in Oracle

I'm trying to parse years entered as strings (please don't get me started - it is what it is). There are years however which are entered that can't be parsed by TO_NUMBER. WITH src AS ( SELECT '2000' AS y FROM DUAL UNION SELECT '1991' AS y…
dokgu
  • 4,957
  • 3
  • 39
  • 77
5
votes
3 answers

Why is it recommended to provide optional radix parameter to parseInt()?

I've always used the parseInt() function in Javascript without passing the radix parameter. As per the MDN documentation here, it's stated that not providing this parameter may result in unpredictable behaviour. Always specify this parameter to…
JSN
  • 2,035
  • 13
  • 27
5
votes
4 answers

JavaScript function parseInt() doesn't parse numbers with leading 0 correctly

I have some zeros prior to a positive integer. I want to remove the zeros so only the positive integer remains. Like '001' will only be '1'. I thought the easiest way was to use parseInt('001'). But what I discovered is that it don't works for the…
einstein
  • 13,389
  • 27
  • 80
  • 110
1 2
3
51 52