Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Convert Long to Int

Hello all,
I need to convert from a long type to a int. I have used intValue() method but I get an error. I have tried to convert long to string with toString() but I get another error. Examples:
((new Date()).getTime()).intValue() --> Error
toString((new Date()).getTime()) --> Error
Can anyone help me?
Regards, Nardinar
Labels (2)
6 Replies
Anonymous
Not applicable
Author

Hello guy
long l1=12l;
int i1=(int)l1;
Best regards

shong
Anonymous
Not applicable
Author

Hi,
I followed the solution given by Shong, as seen in the attached screenshot, yet I still get the error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot cast from Long to int

Has anybody a solution to this?
Anonymous
Not applicable
Author

Hello
Uncheck the 'nullable' option of column.
Best regards
shong
Anonymous
Not applicable
Author

Thank you, Shong!
Unchecking the "Nullable" option was the solution.
In the meanwhile, I found another one, that worked with the "Nullable" option checked:
// to Integer ii from Long nn
ii = nn.intValue();

This does not work when "Nullable" is not checked.

I'd like to know the explanation of this and I'd like to know this kind of details, so that I do not need to ask simple questions on the forum.
Where can I get such information?
Thank you.
Anonymous
Not applicable
Author

Hello
It is basic java knowledge, Long is a class which extends Object class,
intValue() is one of method of Long,
/**
* Returns the value of this <code>Long</code> as an
* <code>int</code>.
*/
public int intValue() {
return (int)value;
}
long is a data type.
Best regards
shong
Anonymous
Not applicable
Author

To handle null values, java ternary operator can be used while converting type in a tMap.
Example : String to BigDecimal
row1.SALARY != null ? new BigDecimal(row1.SALARY) : null

Thanks and Regards,
Pravu Mishra.