# Double + Double = Too Many Decimals

**URL:** <https://forum.katalon.com/t/double-double-too-many-decimals/32653>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [September 5, 2019, 10:35pm UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653 "2019-09-05T22:35:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhomberg](https://avatars.discourse-cdn.com/v4/letter/h/b19c9b/32.png) [@hhomberg](https://forum.katalon.com/u/hhomberg)\
**Post date:** [September 5, 2019, 10:35pm UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/1 "2019-09-05T22:35:32Z")

</div>

I have two fields, **Lodging Base** and **Lodging Tax**. Their Object’s Property “type” equals text, even though the user is only allowed to enter numeric values. After either field loses focus, a dollar sign with a space before the numbers is added to the field.

I set the values in those using

WebUI.setText(findTestObject(‘LodgingBaseRate’), ‘89.01’)  
WebUI.setText(findTestObject(‘LodgingTax’), ‘27.32’)

To add those values together, I’m using

lodgingBase = WebUI.getAttribute(findTestObject(‘LodgingBaseRate’),‘value’).replace(‘$’, ‘’) as Double  
lodgingTaxes = WebUI.getAttribute(findTestObject(‘LodgingTax’),‘value’).replace(‘$’, ‘’) as Double  
lodgingTotal = lodgingBase + lodgingTaxes

When I set the text of a field to lodgingTotal using

WebUI.setText(findTestObject(‘OtherExpenses’), ‘lodging: $’ + lodgingTotal)

…the result in the field is **lodging: $116.33000000000001**.

In my newbie attempts to fix all of those extra decimal places, I’ve tried the below two shots at it, but to no avail to get the answer to also be two decimal places. Any suggestions?

lodgingTotal = lodgingBase + lodgingTaxes as Double

lodgingTotal = (lodgingBase + lodgingTaxes) as Double

---

<div class="post-metadata">

**Author:** ![jason.tolotta](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/jason.tolotta/32/7687_2.png) [@jason.tolotta](https://forum.katalon.com/u/jason.tolotta)\
**Post date:** [September 5, 2019, 11:30pm UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/2 "2019-09-05T23:30:18Z")

</div>

This is the standard behavior of the Double class in Java.

`BigDecimal` is a better choice.

As part of the `BigDecimal` class, Groovy offers a `round` method that accept a `precision` integer value.

```auto
def x = 89.01 as Double
def y = 27.32 as Double

print 'Sum of x + y = ' + (x + y).round(2)

```

This outputs 116.33 BigDecimal value instead of 116.33000000000001

Remove the `round` method and the result will be a Double value.

---

<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:** [September 6, 2019, 12:18am UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/3 "2019-09-06T00:18:58Z")

</div>

Should learn BigDecimal. For example, see

> **[Make cents with BigDecimal](https://www.javaworld.com/article/2075315/make-cents-with-bigdecimal.html)**
>
> Java programs must often calculate and format financial figures. To
> write Java programs that deal with finances, you must know about
> the BigDecimal class and numeric formatting. This
> introductory tutorial shows you how to use BigDecimal
> to calculate...

---

<div class="post-metadata">

**Author:** ![hhomberg](https://avatars.discourse-cdn.com/v4/letter/h/b19c9b/32.png) [@hhomberg](https://forum.katalon.com/u/hhomberg)\
**Post date:** [September 6, 2019, 3:25pm UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/4 "2019-09-06T15:25:59Z")

</div>

Thanks so much for the info and opening my eyes to the BigDouble class!

---

<div class="post-metadata">

**Author:** ![hhomberg](https://avatars.discourse-cdn.com/v4/letter/h/b19c9b/32.png) [@hhomberg](https://forum.katalon.com/u/hhomberg)\
**Post date:** [September 6, 2019, 4:33pm UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/5 "2019-09-06T16:33:31Z")

</div>

@jason.tolotta Following up on this, I changed my line to

lodgingTotal = (lodgingBase + lodgingTaxes).round(2)

…and that worked perfectly. But, it just so happened that after I implemented that fix, I updated my Java SDK to 12.0.2. After the update, the above code change didn’t work, with the reason shown as:

groovy.lang.MissingMethodException: No signature of method: java.math.BigDecimal.round() is applicable for argument types: (java.lang.Integer) values: [2]  
Possible solutions: round(java.math.MathContext), find(), pow(int), find(groovy.lang.Closure), mod(java.lang.Number), and(java.lang.Number)

I went through the suggested link provided by @kazurayam, resulting in changing the code line to

lodgingTotal = (lodgingBase + lodgingTaxes).setScale(2, BigDecimal.ROUND\_HALF\_UP)

…which works perfectly.

---

<div class="post-metadata">

**Author:** ![Ibus](https://avatars.discourse-cdn.com/v4/letter/i/e9a140/32.png) [@Ibus](https://forum.katalon.com/u/Ibus)\
**Post date:** [September 6, 2019, 4:49pm UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/6 "2019-09-06T16:49:20Z")

</div>

@hhomberg katalon does not suport java greater than 8. aka 1.8.xx. at all. so … why would you want to try java 12 with it?  
doing that you are on your own,nobody will take a look into an unsuported code …

---

<div class="post-metadata">

**Author:** ![hhomberg](https://avatars.discourse-cdn.com/v4/letter/h/b19c9b/32.png) [@hhomberg](https://forum.katalon.com/u/hhomberg)\
**Post date:** [September 6, 2019, 5:00pm UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/7 "2019-09-06T17:00:40Z")

</div>

Aha. Good to know that, too!

---

<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:39am UTC](https://forum.katalon.com/t/double-double-too-many-decimals/32653/8 "2022-08-08T07:39:58Z")

</div>

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


