Change string decimal number (2.9) to Int or Long issues

Ok, I'm pretty new to java, but I'm learning fast (hopefully). So, here is my problem:

I have a string (for example, we will use 2.9), I need to change this to int or long or something similar that I can use to compare with another number.

As far as I know, int doesn't support decimals, I'm not sure how long it does? If not, I need to know what decimal places support.

This is the error: java.lang.NumberFormatException: For input string: "2.9" with Interger.parseInt and Long.parseLong

Therefore any help would be appreciated!

+4
source share
4 answers

Both int and long are integer values ​​(being a long representation of a long integer, which is an integer with a larger capacity). Parsing is not performed because these types do not support the decimal part.

If you used them and applied casting, you refuse the decimal part of the number.

 double iAmADouble = 100 / 3; int iWasADouble = (int)iAmADouble; //This number turns out to be 33 

Use double or float .

+1
source

You cannot directly get int (or) long from a decimal point.

One approach:

First get a double value, and then get an int (or) long.

Example:

 int temp = Double.valueOf("20.2").intValue(); System.out.println(temp); 

output:

 20 
+5
source

int and long are integer data types, 32-bit and 64-bit, respectively. You can use float or double to represent floating point numbers.

+4
source

This line (2.9) is neither integer nor long . You must use some decimal points, for example float or double .

+3
source

All Articles