# What is the standard library to use when dealing with money in Kotlin?

**URL:** <https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925>\
**Category:** Libraries\
**Created:** [February 18, 2025, 9:10pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925 "2025-02-18T21:10:17Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 9:10pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/1 "2025-02-18T21:10:17Z")

</div>

I am trying to represent the value of money that supports other currencies and conversion between them and has 100% precision in mathematical operation. Is it better to build an implementation to represent money from the ground up or is there a standard library to use?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [February 18, 2025, 9:42pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/2 "2025-02-18T21:42:10Z")

</div>

You mean conversion like dollars to cents or dollars to euros? The first is usually performed by using `BigDecimal` (Java only) or simply integers and representing values in the “smallest” currency. The second requires some kind of an internet service.

---

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 9:58pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/3 "2025-02-18T21:58:16Z")

</div>

I mean a type that represents monetary values. I would assume some standard libraries are used for monetary representation as there would be precision issues when doing operations, no?

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [February 18, 2025, 10:04pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/4 "2025-02-18T22:04:47Z")

</div>

I think generally most apps represent currency as an integer value. My company has an internal `Money` class which is just a BigInteger (representing the amount) and the currency type.

---

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 10:53pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/5 "2025-02-18T22:53:15Z")

</div>

Thank you for the help. How do you deal with the decimal values without having issues with precision?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [February 18, 2025, 10:55pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/6 "2025-02-18T22:55:35Z")

</div>

I said this already. Either `BigDecimal` or integers using the smallest currency. There are no hidden corner cases or anything otherwise tricky here.

---

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 11:15pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/7 "2025-02-18T23:15:33Z")

</div>

This JUnit test fails:

```auto
@Test
fun test_decimal_() {
    assertEquals((BigDecimal(0.01) + BigDecimal(0.06)), BigDecimal(0.07))
}

```

When a user receives some money, I want the values to be exact, hence my original question. My app is a budgeting tool, but I still value precision for scenarios like these.

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [February 18, 2025, 11:18pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/8 "2025-02-18T23:18:14Z")

</div>

You need to use the `String` consturctor:  
`BigDecimal("0.01")`

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [February 18, 2025, 11:20pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/9 "2025-02-18T23:20:14Z")

</div>

Also, I suggest not using `equals()` and instead use `compareTo() == 0`. It would work in this case, it wouldn’t if scale is different.

---

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 11:20pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/10 "2025-02-18T23:20:55Z")

</div>

This JUnit test brakes in your case:

```auto
@Test
fun test_bin() {
   assertEquals((BigDecimal("1") / BigDecimal("3")), BigDecimal("0.33"))
}

```

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [February 18, 2025, 11:21pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/11 "2025-02-18T23:21:59Z")

</div>

Because that’s incorrect. 1/3 = 0.33333333…, not “0.33”. If you want rounding, you need to specify rounding.

---

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 11:22pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/12 "2025-02-18T23:22:09Z")

</div>

The issue isn’t with the equals, it is with the fact the addition fails.

---

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 11:23pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/13 "2025-02-18T23:23:11Z")

</div>

If you ran the example, you would get 0 for the result:  
This JUnit test also fails:

```auto
@Test
fun test_bin() {
   assertEquals((BigDecimal("1") / BigDecimal("4")), BigDecimal("0.25"))
}

```

(You are right about the division, and I thought the assert would do the rounding for me)

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [February 18, 2025, 11:26pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/14 "2025-02-18T23:26:30Z")

</div>

Just do: `assertEquals((BigDecimal("1.00") / BigDecimal("4.00")), BigDecimal("0.25"))`. But most importantly: read the documentation for `BigDecimal` if you plan to use it.

---

<div class="post-metadata">

**Author:** ![DanielCoder834](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@DanielCoder834](https://discuss.kotlinlang.org/u/DanielCoder834)\
**Post date:** [February 18, 2025, 11:55pm UTC](https://discuss.kotlinlang.org/t/what-is-the-standard-library-to-use-when-dealing-with-money-in-kotlin/29925/15 "2025-02-18T23:55:09Z")

</div>

This works. Thank you for the help.
