Skip to main content
Announcements
July 15, NEW Customer Portal: Initial launch will improve how you submit Support Cases. IMPORTANT DETAILS
cancel
Showing results for 
Search instead for 
Did you mean: 
SMahadevan1608028474
Contributor
Contributor

How to Convert String to Decimal format

My source is csv file and I have one column say (Eg: Product Price) It has 1456.45667 as value in csv file. While reading the file in Talend this column treat as String. But I want to check the format as decimal.

I am expecting the output as 1456.45 (Not even round value). expecting it should accept only two digits after decimal. Even I have tried to change the Datatype and it shows the error as Cannot Convert String to Decimal.

Labels (2)
18 Replies
CLi1594691515
Contributor
Contributor

Hi,

 

If you are using tMap, input String and output is float, you need to perform parsing dataType to do so.

In your case, you may use Double.parseDouble(String) whichever data type you need to convert to.

 

If you want to read it as float/double, you can set it in schema in input component.0693p00000BE2ylAAD.png

SMahadevan1608028474
Contributor
Contributor
Author

Thanks for your reply. But Still am facing this issue.

 

tMap

*******

<Product Price> - 14.5674 (It is reading as String) -Based on my source data

 

Output table

****************

I am using this below syntax in output table expression Builder,

Double.parseDouble(Product Price) - I have changed this column datatype as "Double" and I have set precision as 2. It is not throwing the error after run job. But the value appear in table as 14.5674 instead of 14.56.

 

CLi1594691515
Contributor
Contributor

Hi

 

I got your point.

 

We can make some work around.

In tMap.

Create a Var in the mid section (int) (Double.parseDouble(row8.product_price)*100 ) which result should be 1456.

In the output, Double.parseDouble(String.valueOf(Var.var1))/100f which result would be 14.56

 

The concept is whenever the decimal place you need to cutoff instead of rounding, you just need to multiply by 100 (depends on how many decimal place you wants) and convert to Integer, and divide by the number multiply by before to return them to your desired value.

 

 

SMahadevan1608028474
Contributor
Contributor
Author

Hi,

 

Thanks for your response, I have tried the way you explained but still am not getting the expected output.

 

Source

**********

Product Price - 14.6783 (String Datatype)

 

I have created the Var as below,

Double.parseDouble(row4.Transaction_LINE_Quantity)*100

(Choose the datatype as Int)

 

Output table

**************

Double.parseDouble(String.valueOf(Var.var1))/100

(Set datatype as Double)

After run the job the below error am getting

Type Mismatch: "Cannot convert from double to int"

CLi1594691515
Contributor
Contributor

Hi,

 

here is the example.

 

tMap

0693p00000BEFFSAA5.png 

Result0693p00000BEFFfAAP.png

SMahadevan1608028474
Contributor
Contributor
Author

THANKS IT'S WORKING FINE AS EXPECTED. I have listed out the below.

Product Price - 14.6783 (String Datatype)

I have created the var as below,

(int)(Double.parseDouble(row4.Product price)*100) (Set datatype as Int)

Output table

**************

Double.parseDouble(String.valueOf(Var.var1))/100 (Set datatype as Double and precision 2)

I am getting the expected output as 14.67

 

SMahadevan1608028474
Contributor
Contributor
Author

But the same scenario I want another field accept only 4 decmial place and I don't want to round up the values.

Product Price - 0.91275 (String Datatype)

I have created the var as below,

(int)(Double.parseDouble(row4.Product price)*100) (Set datatype as Int)

Output table

**************

Double.parseDouble(String.valueOf(Var.var1))/100 (Set datatype as Double and precision 4)

I am expecting the value in csv file as (0.9127) instead of 0.91. It is getting roundup the value.

CLi1594691515
Contributor
Contributor

for example:

2 decimal place 100

4 decimal place 10000

the control of dp of the value itself is not depends on the precision setting

 

(int)(Double.parseDouble(row4.Product price)*10000)

Double.parseDouble(String.valueOf(Var.var1))/10000

SMahadevan1608028474
Contributor
Contributor
Author

Thanks for your help.

The 4 decimal places it's getting displayed (0.91275) - 0.9127

for 2 decimal places it's getting appear as (30.7474) - 30.74