Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
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
SMahadevan1608028474
Contributor
Contributor
Author

Source

-------

Product price

This is column is not mandatory and it have null values also in source data and it's reading the data as String.

 

I have tried the below ways, but still am facing the Empty String issue.

 

(row4.Unit_Selling_Price.equals(null)?null:(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000))

 

(row4.Unit_Selling_Price!=(null)?(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000):null)

 

(row4.Unit_Selling_Price.equals(null)||row4.Unit_Selling_Price!=(null)?(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000):null)

 

CLi1594691515
Contributor
Contributor

".equals()" doesn't apply to null value. you can use row4.Unit_Selling_Price == null instead

SMahadevan1608028474
Contributor
Contributor
Author

I tried with this statement,

((row4.Unit_Selling_Price==null)?null:(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000))

 

But the exception is occur,

Exception in component tMap_1 (Datavalidation_Filter_V1_3_tconvertype)

java.lang.NumberFormatException: empty String

 at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)

 at sun.misc.FloatingDecimal.parseDouble(Unknown Source)

 at java.lang.Double.parseDouble(Unknown Source)

 at

Prakhar1
Creator III
Creator III

--For Empty use below condition , for NULL use above

and for both use both the condition as "OR"

(((row4.Unit_Selling_Price)isEmpty())?null:(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000))

SMahadevan1608028474
Contributor
Contributor
Author

my source csv file has blank value (i.e. Empty), So I have tried the below statements,

 

(((row4.Unit_Selling_Price.isEmpty())?null:(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000)))

 

am getting the below error,

Exception in component tMap_1 (Datavalidation_Filter_V1_3_tconvertype)

java.lang.NullPointerException

 at datavalidation_jan11.datavalidation_filter_v1_3_tconvertype_0_1.Datavalidation_Filter_V1_3_tconvertype.tFileInputDelimited_1Process(Datavalidation_Filter_V1_3_tconvertype.java:52769)

 

 

Even I have tried with both ,

(((row4.Unit_Selling_Price.isEmpty()||row4.Unit_Selling_Price==null)?null:(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000)))

 

But still facing the same issues.

Prakhar1
Creator III
Creator III

Why you are making the true value as "NULL" ?

Can you try to make it like 0 or something else

like

(((row4.Unit_Selling_Price.isEmpty()||row4.Unit_Selling_Price==null)?0:(int)(Double.parseDouble(row4.Unit_Selling_Price)*10000)))

SMahadevan1608028474
Contributor
Contributor
Author

I have tried with this statement and it's working fine. Thanks for your response on this Empty String.

 

I have one more clarification,

 

I have the source column as Product_Amount as Float Datatype (Eg: 23.6161)

I am expecting this should load only the 2 decimal values (Eg: 23.61).

In Var

******

(int)(Float.parseFloat(row4.Product_Amount)*100) - Datatype is Int

Here I need to cut off the amount and need to multiply by 100 to convert to integer.

In output table

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

Float.parseFloat(String.valueOf(Var.var9))/100 - Type is Float

While running the program I got the error as attached.

 

Prakhar1
Creator III
Creator III

It means you are passing a Float value in the parseFloat() function.

you should pass a string value in it or other data type value

(int)(Float.parseFloat(row4.Product_Amount)*100)

In the above statement as you mentioned row4.Product_Amount is of type Float, so it is wrong statement.

 

 

SMahadevan1608028474
Contributor
Contributor
Author

Source:

**********

row4.Product_Amount -String Data type.

I have changed the datatype in source from Float to String.

In Var

*******

(int)(Float.parseFloat(row4.Transaction_LINE_Amount)*100))) - Datatype int

 

Output table

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

Float.parseFloat(String.valueOf(Var.var10))/100

 

still the same issue occurs... Can you please let me know where am doing the mistake...