From Thursday, July, 9, 3:00 PM Pacific,
our Community site will be in
read-only mode
through Sunday, July 12th.
Thank you for your patience.

Format value to two decimal places without rounding

Highlighted
Seven Stars

Format value to two decimal places without rounding

Hello all,

 

I want to format the below value to two decimal places without rounding it.

 

Example:

 

ColumnAColumnBResult
171.4562.321169.135
25.4585610.542614.91596

 

For info, Column A, B, and result is of type String and this result datatype in my output to remain as String.

 

My expected output should be 169.13 and 14.91

 

I have tried with the two option below:

 

 

String.format("%.2f", (Double.parseDouble(ColumnA) - Double.parseDouble(ColumnB)))

 

 

String.format("%.2f", (new BigDecimal(Var.var2).subtract(new BigDecimal(Var.var3)))) 

 

 

But it is rounding the value to: 169.14 and 14.92

 

Also, i want to know what is the difference between Big Decimal and Double? What should i use in my situation?

 

Thank you for your help.

 

Best regards,

Asadasing


Accepted Solutions
Highlighted
Nine Stars

Re: Format value to two decimal places without rounding

Hi,

 

Please try below.

 

Decimal1.PNGDecimal2.PNG

Regards,

Veeru Boppudi

View solution in original post

Highlighted
Moderator

Re: Format value to two decimal places without rounding

@asadasing,try with below way and let me know,if you are still facing issue.

 

row3.newColumn2==null | row3.newColumn2.equals("")?"0"Smiley Sadrow3.newColumn2.replace(",",".")).replace(" ","")

Manohar B
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.

View solution in original post


All Replies
Highlighted
Nine Stars

Re: Format value to two decimal places without rounding

Hi,

 

Please try below.

 

Decimal1.PNGDecimal2.PNG

Regards,

Veeru Boppudi

View solution in original post

Highlighted
Seven Stars

Re: Format value to two decimal places without rounding

Hello Veeranjaneyulu,
Thank you for the solution. It works.
However, do you know the difference between bigdecimal and double?

How can you determine the usage of one of these?\

Regards,
Asadasing
Highlighted
Seven Stars

Re: Format value to two decimal places without rounding

@vboppudi

Tried your solution and encountering a blocking error.
Please see link below:
https://community.talend.com/t5/Design-and-Development/Tmap-Error-NumberFormatException-For-input-st...

Thank you for your help.
Highlighted
Moderator

Re: Format value to two decimal places without rounding

@asadasing,try with below way and let me know,if you are still facing issue.

 

row3.newColumn2==null | row3.newColumn2.equals("")?"0"Smiley Sadrow3.newColumn2.replace(",",".")).replace(" ","")

Manohar B
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.

View solution in original post

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Put Massive Amounts of Data to Work

Learn how to make your data more available, reduce costs and cut your build time

Watch Now

How OTTO Utilizes Big Data to Deliver Personalized Experiences

Read about OTTO's experiences with Big Data and Personalized Experiences

Blog

Talend Integration with Databricks

Take a look at this video about Talend Integration with Databricks

Watch Now