String to Float conversion

One Star

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!
One Star

Re: String to Float conversion

I would try to use BigDecimal instead of float and set the precision to 2
One Star

Re: String to Float conversion

Hi lubod,
I've tested with BigDecimal but same result Smiley Sad
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.

Re: 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!

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
Five Stars

Re: String to Float conversion

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.
One Star

Re: String to Float conversion

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!
Five Stars

Re: String to Float conversion

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
One Star

Re: String to Float conversion

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!
Five Stars

Re: String to Float conversion

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.
One Star

Re: String to Float conversion

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.
Five Stars

Re: String to Float conversion

Hi Rozie,
All right here are steps to create all things step by step.
1. Create a routine named with "NumericConversion"
2. paste below function in it.
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);
}

3. import required name spaces.
4. open tMap and use function like below.
yourcolumn!=null?
NumericConversion.GetDecimalFromFixedString(yourcolumn,decimalpointposition):new BigDecimal("0")

I am busy in some work so writing in very short words.
One Star

Re: String to Float conversion

Hi umeshrakhe,
I'm sorry if my prob keeps bugging you. I'm however still receiving error.
My code follows exactly as you are:
package routines;
public class NumericConversion {
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);
}
}

Attached are the image files of the errors I received.
Regards,
Rozie
Community Manager

Re: String to Float conversion

Hi Rozie
It is a compilation error here, use java.util.BigDecimal instead of BigDecimal in your routine
for example:
 BigDecimal Result= new BigDecimal("0.00");

to
 java.math.BigDecimal Result= new java.math.BigDecimal("0.00");

Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business