Five Stars

Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.

 Hi All,

 

I have got this error while loading data, I tried with java syntax new BigDecimal: Kindly help me.

 

Error: Cannot Convert from float to BigDecimal.

 

Talend Team Please comment on this.

 

Error.pngError_1.png 

7 REPLIES
Employee

Re: Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.

I'm not sure the error is happening where you think it is.   Perhaps switch to the code view and see more info on where it's happening. 

 

Also, converting from a float to a BigDecimal isn't perfect since float is not an exact numeric. You may want to consider rounding to specific number of digits to get more exact.  What you show should compile, though. 

 

new BigDecimal(field, new MathContext(3, RoundingMode.HALF_EVEN))

 

Five Stars

Re: Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.

Thanks for reply.

 

Code error is Exception with Type Mistmatch: Cannot convert Float to BigDecimal.

 

tried our code too but error not resolved.

Request talend team to look into this.

 

 

Employee

Re: Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.


jagadesh-raj wrote:

 

tried our code too but error not resolved. 


 

Hi, I think the error is occurring somewhere else in our job (not what you are showing in your screenshot). I was suggesting to look at the code tab to see more specifically where the error is. 

 

 

 

 

 

Six Stars

Re: Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.

Hi

 

Have you used TypeConvert routine in Talend?

 

Please try with TypeConvert.Float2BigDecimal(floatvalue)  where floatvalue is the value from source and assign the output to Target.

 

Velu.

Five Stars

Re: Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.

Hi Velu,

 

Thanks for your reply, I'm getting same Type Mismatch error again no change at all with your expression.

 

Regards,

Jagadesh

Six Stars

Re: Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.

Hi

 

Try converting with BigDecimal.valueOf(float) to get the BigDecimal value

 

Regards

Velu

Seven Stars

Re: Cannot Convert from Float to BigDecimal Error: Tried with java syntax new BigDecimal.

Hi there,

 

There is no BigDecimal constructor which takes a Float, although there is one which takes a Double, and Java should automatically promote your Float to a Double when used within the constructor.

 

You also need to consider what happens if your input value is null. Use a ternary expression to return BigDecimal.ZERO instead, which for math purposes is likely to be what you'll want.

 

Be aware that converting from a Float to a BigDecimal will almost certainly not give you the exact value you're expecting, due to lack of precision with the floating point data type.

 

For example, if your input value is 1.23f, then converting to a BigDecimal without specifying the precision will give you the value 1.230000019073486328125, which probably isn't what you want. By adding a suitable MathContext to your BigDecimal constructor (e.g. new BigDecimal(floatIn, new java.math.MathContext(2))) you'll get 1.23.

 

All that said, as @mverrilli says, it's still quite possible that the error you're seeing is occurring elsewhere in the job, so click "Continue" on the error dialog, make a note of the line number given in the stack trace, then switch to the Code pane and check that the error is actually occurring on the expression within you tMap.

 

Regards,

 

 

Chris