One Star

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
6 REPLIES
Community Manager

Re: Convert Long to Int

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

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Convert Long to Int

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?
Community Manager

Re: Convert Long to Int

Hello
Uncheck the 'nullable' option of column.
Best regards
shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Convert Long to Int

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.
Community Manager

Re: Convert Long to Int

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
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Convert Long to Int

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.