One Star

Convert string to integer or float

Hi,
Do you Know an instruction to transform a string to an integer or a float in Tmap?
Thanks

17 REPLIES
Employee

Re: Convert string to integer or float

Hello
You can try Integer.parseInt("10") or Float.parseFloat("10.0")
Regards,
One Star

Re: Convert string to integer or float

Hi mhirt,
With Float.parseFloat(?10.0?), I have the following message
Expression of Var.var1 is invalid : Error on Float.parseFloat(?10.0?) => Type mismatch : cannot convert from float to String
Thanks
Employee

Re: Convert string to integer or float

Change the type of your column Var.var1 from "String" type to "float" or "Float" type and it should work :-)
One Star

Re: Convert string to integer or float

Thanks amaumont
One Star

Re: Convert string to integer or float

Hi,
Do you Know an instruction to transform a Float or integer to a String in Tmap?
Thanks
Employee

Re: Convert string to integer or float

This is:
String.valueOf(yourValue)

or
"" + yourValue
One Star

Re: Convert string to integer or float

Thanks amaumont
One Star

Re: Convert string to integer or float

Hi all,
I have a HOME PHONE NUMBER as a STRING that I want to only get the first 3 digits (AREACODE) of and then want to convert to an INT..
Right now im using the following and getting an error:
I create a variable as so...
StringHandling.LEFT(row1.homePhoneNum,3) which is called VAR.SDHAREA(STRING)
THEN...
I map it to my output and try to convert it to an int as follows...
Integer.parseInteger(VAR.SDHAREA)

Anyone have any ideas on how to accomplish this?
Any help would be greatly appreciated!
-Chris
Employee

Re: Convert string to integer or float

Hi chrish:
JDK API (http://java.sun.com/j2se/1.5.0/docs/api/) :
Class: java.lang.Integer
static int parseInt(String s)
Parses the string argument as a signed decimal integer.
you should try Integer.parseInt(VAR.SDHAREA)
One Star

Re: Convert string to integer or float

Have a problem... try to convert string to int like this "5 600" from xls file. shows error
xception in component tMap_1
java.lang.NumberFormatException: For input string: "5 250"
at java.lang.NumberFormatException.forInputString(Unknown Source)
try to do like this
row1.Hotel_commissions.toString().equals("#") || row1.Hotel_commissions.toString().equals("") ? null :
Integer.parseInt(row1.Hotel_commissions.toString().replace(" ",""))
this not help

Re: Convert string to integer or float

ug. Java's not the best for string manipulation. in perl the correct solution would be:
my $input = "5 250"
my $output = do { $input =~ s/\s//g; };

I highly suggest checking out the Perl version of Talend. Perl was built for ETL and Talend makes writing powerful ETL scripts very fast.
One Star

Re: Convert string to integer or float

this is right if you want to drop.
Right conversion is made with Math.rint() or Math.round().or you can do it by hand:
float f = 234.33;
int i = (int)(f + 0.5f);
i has value 234
float f = 234.57;
int i = (int)(f + 0.5f);
i has value 235;
One Star

Re: Convert string to integer or float

Hi,
I want to parse a string and change the format as given below,
Input : C0003129872
Outpput : C=3129872
Could anyone tell me how can this be obtained pls?
One Star

Re: Convert string to integer or float

Hi baskarchennai,
You can do it by using this code:
StringHandling.CHANGE("C0003129872", "000", "=")

Regards,
Khor.
One Star

Re: Convert string to integer or float

Hello,
I would like to know how I do in Talend to calculate the age of a person from his date of birth?
I know we need to use the current date, but I look for the syntax.
Thank you in advance
One Star

Re: Convert string to integer or float

Don't post the same question more than once - http://www.talendforge.org/forum/viewtopic.php?id=31157.
One Star

Re: Convert string to integer or float

More about......String to Integer
Biden