Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

String to Float conversion

Hi Talend community,
I have a problem with converting from String to Float.
This is my input:
3 11 29 0 01020 50 8101 0100 0000 0 000
0 00 00 0 00000 00 0000 0000 0000 0 000
0 00 00 0 00000 00 0000 0000 0000 0 000
Expected output:
3 11 29 0 01020 50 81.01 01.00 00.00 0 000
0 00 00 0 00000 00 00.00 00.00 00.00 0 000
0 00 00 0 00000 00 00.00 00.00 00.00 0 000

I've tried with Float parse and put precision of 2 at tMap. But the output only display 0. Does this has something to do with 0 value.
Thanks in advance!
Labels (2)
12 Replies
Anonymous
Not applicable
Author

I would try to use BigDecimal instead of float and set the precision to 2
Anonymous
Not applicable
Author

Hi lubod,
I've tested with BigDecimal but same result 0683p000009MPcz.png
Please help me how.
Current output:
-------------------
3 11 29 0 01020 50 8101 100 0 0 000
0 00 00 0 00000 00 0000 0 0 0 000
0 00 00 0 00000 00 0000 0 0 0 000
Many thanks in advance.
Anonymous
Not applicable
Author

Hi Talend community,
I have a problem with converting from String to Float.
This is my input:
3 11 29 0 01020 50 8101 0100 0000 0 000
0 00 00 0 00000 00 0000 0000 0000 0 000
0 00 00 0 00000 00 0000 0000 0000 0 000
Expected output:
3 11 29 0 01020 50 81.01 01.00 00.00 0 000
0 00 00 0 00000 00 00.00 00.00 00.00 0 000
0 00 00 0 00000 00 00.00 00.00 00.00 0 000

I've tried with Float parse and put precision of 2 at tMap. But the output only display 0. Does this has something to do with 0 value.
Thanks in advance!

Hi Rozie,
You can do this even without using tMap by changing the schema of the input file column to BigDecimal with precision 2. If you want to do this using tMap then you need to change both the input and the output schema columns of tMap to BigDecimal with precision 2.
Best Regards,
Diwakar
Anonymous
Not applicable
Author

Create a function like below
GetDecimalFromFixedString(String Column,int Precision)
{
BigDecimal Result= new BigDecimal("0.00");
String s = Column.substring(0,((Column).length()- Precision));

String ss =Column.replace(s, "");
Result =new BigDecimal(s+"."+ss);
return Result =new BigDecimal(s+"."+ss);
}

Handel exceptions and errors which may come if your input string null or empty.
Anonymous
Not applicable
Author

Hi umeshrakhe,
I'm sorry but where should I put the function code at? Create new routine or tJava?
Could you please show me steps of doing so?
Thanks in advance!
Anonymous
Not applicable
Author

create a new routine and place above function, and then call this function using tMap or Tjavarow where you want to convert the values. best way is use tMap.
public static BigDecimal GetDecimalFromFixedString(String Column,int Precision)
{
BigDecimal Result= new BigDecimal("0.00");
String s = Column.substring(0,((Column).length()- Precision));

String ss =Column.replace(s, "");
Result =new BigDecimal(s+"."+ss);
return Result =new BigDecimal(s+"."+ss);
}

in tMap use like below.
value=121212
GetDecimalFromFixedString(row1.Inv_qty,2)

result 1212.12
Anonymous
Not applicable
Author

Hi umeshrakhe,
I've created a new function and named it as GetDecimalFromFixedString as below:
package routines;
import java.math.BigDecimal;

public class GetDecimalFromFixedString
{
public static BigDecimal GetDecimalFromFixedString(String Column,int Precision)
{
BigDecimal Result= new BigDecimal("0.00");
String s = Column.substring(0,((Column).length()- Precision));

String ss =Column.replace(s, "");
Result =new BigDecimal(s+"."+ss);
return Result =new BigDecimal(s+"."+ss);
}

}

But my job returns error. I wonder what's wrong as I followed like you said.
Thanks a lot!
0683p000009MDwY.jpg 0683p000009MDwd.jpg
Anonymous
Not applicable
Author

GetDecimalFromFixedString.GetDecimalFromFixedString(String Column,int Precision)

try above one. one more suggestion, change the class name with something like NumericConversion so all the function will goes to this routine it will help you to manage and all the function.
try this will help to sort the problem.
Anonymous
Not applicable
Author

Hi umeshrakhe,
Thanks for your help.
I'm sorry, I'm totally newbie to Java so could you show me the full code. Seems like I keep getting error. -_-'
Appreciate your feedback.