# Decimal floating point arithmetic

**URL:** https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447
**Category:** Learning
**Created:** [January 19, 2018, 2:22pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447 "2018-01-19T14:22:06Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Haudegen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/haudegen/32/716_2.png) [@Haudegen](https://discuss.ocaml.org/u/Haudegen)
#### Post date: [January 19, 2018, 2:22pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/1 "2018-01-19T14:22:06Z")

</div>

Is there a decimal data type in OCaml which doesn’t have the classic binary representation problem?

```
# 1.1 +. 2.2;;
- : float = 3.3000000000000003

```

Thank you!

---

<div class="post-metadata">

### Author: ![thizanne](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/thizanne/32/764_2.png) [@thizanne](https://discuss.ocaml.org/u/thizanne)
#### Post date: [January 19, 2018, 2:33pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/2 "2018-01-19T14:33:54Z")

</div>

You can use Zarith [1]. Note that it works with rational numbers, not  
"decimal" stricto sensu, but it might suit your needs.

[1] [https://github.com/ocaml/Zarith](https://github.com/ocaml/Zarith)

---

<div class="post-metadata">

### Author: ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)
#### Post date: [January 21, 2018, 8:57pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/3 "2018-01-21T20:57:32Z")

</div>

This said, some people doing finance calculations need decimal rather than binary representations. Gnu MP won’t do it, but GNU MPFR will.

---

<div class="post-metadata">

### Author: ![Haudegen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/haudegen/32/716_2.png) [@Haudegen](https://discuss.ocaml.org/u/Haudegen)
#### Post date: [January 22, 2018, 11:45am UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/4 "2018-01-22T11:45:58Z")

</div>

That’s my use case indeed. I have to work with a lot of small monetary amounts and I can’t afford rounding (or representation) errors. The database provides and accepts numeric/decimal values.  
Which data type do seasoned OCamlers use for calculations with monetary amounts?  
Thank you all!

---

<div class="post-metadata">

### Author: ![n4323](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@n4323](https://discuss.ocaml.org/u/n4323)
#### Post date: [January 22, 2018, 12:01pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/5 "2018-01-22T12:01:44Z")

</div>

+1 for latin declination 🙂

---

<div class="post-metadata">

### Author: ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)
#### Post date: [January 22, 2018, 4:59pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/6 "2018-01-22T16:59:21Z")

</div>

I think mostly people have been using arbitrary precision types and haven’t worried much about decimal vs. binary representation issues. You probably can get away with having arbitrary precision integers of the smallest unit of account (say, cents) that’s relevant, but yes, doing a variation of the arbitrary precision package that did decimal might be a good thing for the small subset of users that really need it.

---

<div class="post-metadata">

### Author: ![UnixJunkie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/unixjunkie/32/638_2.png) [@UnixJunkie](https://discuss.ocaml.org/u/UnixJunkie)
#### Post date: [June 28, 2023, 1:24am UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/7 "2023-06-28T01:24:37Z")

</div>

Multiply all your values by 10 (or whatever the smallest coin value requires), and work with integers.

---

<div class="post-metadata">

### Author: ![Haudegen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/haudegen/32/716_2.png) [@Haudegen](https://discuss.ocaml.org/u/Haudegen)
#### Post date: [June 28, 2023, 8:26am UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/8 "2023-06-28T08:26:23Z")

</div>

Won’t work if you need correct rounding.

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [June 28, 2023, 1:46pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/9 "2023-06-28T13:46:10Z")

</div>

Just fyi [Kind of "financial" integer printing format - #8 by yawaramin](https://discuss.ocaml.org/t/kind-of-financial-integer-printing-format/12460/8)

---

<div class="post-metadata">

### Author: ![Haudegen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/haudegen/32/716_2.png) [@Haudegen](https://discuss.ocaml.org/u/Haudegen)
#### Post date: [June 28, 2023, 6:23pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/10 "2023-06-28T18:23:01Z")

</div>

Thank you, I’ve been using “decimal” for a long time now. I should have marked this as an “answered question”, sorry, my bad. I’m still not used to these fancy new webforums. 🙂

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [June 28, 2023, 6:28pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/11 "2023-06-28T18:28:49Z")

</div>

No problem, I just wanted to put it out there in case someone came across this thread later and got the impression OCaml doesn’t have a decimal type.

---

<div class="post-metadata">

### Author: ![erikmd](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/erikmd/32/2172_2.png) [@erikmd](https://discuss.ocaml.org/u/erikmd)
#### Post date: [July 27, 2023, 8:59pm UTC](https://discuss.ocaml.org/t/decimal-floating-point-arithmetic/1447/12 "2023-07-27T20:59:13Z")

</div>

@perry previously mentioned [MPFR](https://www.mpfr.org/), so FYI it happens there exists some OCaml binding for MPFR:

> **[GitHub - thvnx/mlmpfr: OCaml bindings for MPFR](https://github.com/thvnx/mlmpfr)**
>
> OCaml bindings for MPFR. Contribute to thvnx/mlmpfr development by creating an account on GitHub.
