askIT

Android Convert long to int

You can change a number from data type long to int using the code below.

Long to int

long myLong = 5;
int myInt = (int)myLong

Example of changing long to int:

The code here will first check if the number is to small or to big for the data type int.

public static int safeLongToInt(long l) {
        if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
                throw new IllegalArgumentException
                        (l + " cannot be cast to int without changing its value.");
        }
        return (int) l;
}

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.