August 29, 2017
How to convert between different datatypes
The following table is a set of Java and Talend methods that allow conversion between
different datatypes. This is by no means an exhaustive list, but will cover many of type
conversions that you will encounter. You can use them in the tMap, tXMLmap or tJava component for example.
From type | To Type | Example |
---|---|---|
String | Integer | Integer.parseInt(row1.myString) |
String | Integer | (new Integer(row1.myString)).toString() |
String | Date | TalendDate.parseDate("dd-MMyyyy", row1.myString) |
String | BigDecimal | new BigDecimal(row1.myString) where myString can include decimal places. For example, 99.00 |
String | Float | Float.parseFloat(row1.myString) |
String | Long | Long.parseLong(row1.myString) |
Integer | String | variable+"" or variable.toString() |
Date | String | TalendDate.formatDate("yy-MM-dd", row1.myDate) |
BigDecimal | String | row1.myBigDecimal.toString() |
Float | String | row1.myFloat.toString() |
Long | String | row1.myLong.toString() |
Integer | Long | row1.myInteger.longValue() |
Integer | BigDecimal | new BigDecimal(row1.myInteger) |
Integer | Float | new Float(row1.myInteger) |
Float | Integer | To do this conversion you need to decide on a rounding methods such as Math.round(), Math.ceil(), Math.floor() and then cast the result to Integer. |
BigDecimal | Integer | As with Float, BigDecimal can have decimal places, so will need to be rounded prior to casting to Integer. |
Float | BigDecimal | new BigDecimal(Float.toString(row1. myFloat)) |
3 Comments