Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a String decimal (2.9) to Int or Long issues

Tags:

java

Okay, I'm fairly new to java but I'm learning quickly(hopefully). So anyway here is my problem:

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

As far as I know int doesn't support decimals, I'm not sure if long does either? If not I need to know what does support decimals.

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

So any help would be appreciated!

like image 367
Tazmanian Tad Avatar asked Dec 11 '22 18:12

Tazmanian Tad


2 Answers

You can't directly get int (or) long from decimal point value.

One approach is:

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

Example:

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

output:

20
like image 156
kosa Avatar answered Dec 26 '22 15:12

kosa


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

like image 40
GriffeyDog Avatar answered Dec 26 '22 15:12

GriffeyDog