# Need help with BigDecimal Precision Issue

**URL:** <https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [May 14, 2019, 10:18pm UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334 "2019-05-14T22:18:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [May 14, 2019, 10:18pm UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/1 "2019-05-14T22:18:39Z")

</div>

Hello Team,

I need help with a BigDecimal Value.

The issue is I have a scenario where I am grabbing a price from website in BigDecimal datatype and then verifying if it is equal to my expected value, both the price and expected value are in BigDecimal. The expected value I am calculating is within the script

```auto
total_item_wise_after_increment_1 = WebUI.getText(findTestObject('4_Shopping_Cart_Page/Total_Item_Wise')).replace('$', '')

BigDecimal total_item_wise_after_increment = new BigDecimal(total_item_wise_after_increment_1)

WebUI.println('Total for the Item on Shopping Cart Page after increment is : ' + total_item_wise_after_increment)

/* Calculating Expected Total */

expected_total_item_wise_after_increment_1 = total_item_wise.multiply(expected_quantity_shopping_cart_after_increment)

BigDecimal expected_total_item_wise_after_increment = new BigDecimal(expected_total_item_wise_after_increment_1)

WebUI.println('The expected total item wise after increment should be : ' + expected_total_item_wise_after_increment)

/* The Script will now verify if expected total item wise is equal to what shows up on the Shopping Cart page */

assert expected_total_item_wise_after_increment.equals(total_item_wise_after_increment)

```

Upon executing the Script I am getting this error :

```auto
Assertion failed: 

assert expected_total_item_wise_after_increment.equals(total_item_wise_after_increment)
       | | |
       | false 242.45
       242.44999999999998863131622783839702606201171875

```

I tried to Use Rounding method but that doesn’t work all the time, The price will change every time I run the test case.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 15, 2019, 2:30am UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/2 "2019-05-15T02:30:38Z")

</div>

You should learn about the BigDeciaml’s [setScale](https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#setScale(int,%20java.math.RoundingMode)) method.

A tutorial about it:  
[https://www.tutorialspoint.com/java/math/bigdecimal\_setscale\_rm\_roundingmode.htm](https://www.tutorialspoint.com/java/math/bigdecimal_setscale_rm_roundingmode.htm)

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Timo\_Kuisma](https://forum.katalon.com/u/Timo_Kuisma)\
**Post date:** [May 15, 2019, 5:39pm UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/3 "2019-05-15T17:39:57Z")

</div>

hello,

```
i tried with RoundingMode.CEILING --> round up if rounding digit is 5 or greater
242.44999999999998863131622783839702606201171875 after changing the scale to 2 and rounding is 242.45

// create 2 BigDecimal Objects
BigDecimal bg1, bg2;
bg1 = new BigDecimal(242.44999999999998863131622783839702606201171875);
// set scale of bg1 to 2 in bg2 using floor as rounding mode
bg2 = bg1.setScale(2, RoundingMode.CEILING);
String str = bg1 + " after changing the scale to 2 and rounding is " +bg2;
// print bg2 value
System.out.println( str );
output
242.45
```

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [May 15, 2019, 5:45pm UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/4 "2019-05-15T17:45:32Z")

</div>

Are the “expected\_quantity\_shopping\_cart\_after\_increment” and “total\_item\_wise” also BigDecimal objects?

Also, you don’t need this line:

`BigDecimal expected_total_item_wise_after_increment = new BigDecimal(expected_total_item_wise_after_increment_1)`

because the multiply() method from the previous step will return a BigDecimal already.

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [May 15, 2019, 5:54pm UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/5 "2019-05-15T17:54:29Z")

</div>

@Brandon_Hein Yes both are big decimal objects.

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [May 15, 2019, 8:46pm UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/6 "2019-05-15T20:46:42Z")

</div>

The rounding off solution doesn’t work all the time. Here is the scenario

- If the product price is 48.49 and customer buys 2 quantity then the website shows total\_item\_wise\_after\_increment = 96.98

- To calculate the expected\_total\_item\_wise\_after\_increment I am doing total\_item\_wise.multiply(expected\_quantity\_shopping\_cart\_after\_increment) which is giving 96.99

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [May 15, 2019, 9:29pm UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/7 "2019-05-15T21:29:47Z")

</div>

Executing the below code:

```
BigDecimal price = new BigDecimal("48.49");
BigDecimal quantity = new BigDecimal("2");
BigDecimal total = price.multiply(quantity);

```

the value of ‘total’ correctly shows 96.98. While the other comments are correct, you can indeed mess with the rounding strategy, the whole point of using BigDecimal is that you shouldn’t need to worry about it. If you want, you can DM me your code and I can check where the disconnect is.

---

<div class="post-metadata">

**Author:** ![albert.vu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/albert.vu/32/20595_2.png) [@albert.vu](https://forum.katalon.com/u/albert.vu)\
**Post date:** [August 8, 2022, 7:49am UTC](https://forum.katalon.com/t/need-help-with-bigdecimal-precision-issue/25334/8 "2022-08-08T07:49:02Z")

</div>

Archived on August 8, 2022, 7:49am UTC


