Six 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

1 ACCEPTED SOLUTION

Accepted Solutions
Nine Stars

Re: Format value to two decimal places without rounding

Hi,

 

Please try below.

 

Decimal1.PNGDecimal2.PNG

Regards,

Veeru Boppudi
2 REPLIES
Nine Stars

Re: Format value to two decimal places without rounding

Hi,

 

Please try below.

 

Decimal1.PNGDecimal2.PNG

Regards,

Veeru Boppudi
Six 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