Awesome Open Source
Awesome Open Source


Arbitrary-precision floating-point decimal type implemented in OCaml. Ported from Python decimal module. It uses Zarith to do biginteger arithmetic.



This package is licensed under the Python Software Foundation License v2, for the sake of simplicity, as it is a derived work of the Python decimal module.


$ opam install decimal # if trying out
# (* if trying out: *)
  #require "decimal";;
# (* for convenience *)
  module D = Decimal
  let i = D.of_int
  let s = D.of_string;;
# (* tell the REPL how to display decimals *)
  #install_printer D.pp;;
  D.(s "0.1" + s "0.2");;
- : D.t = 0.3
# (* default precision is 32 *)
  D.(i 1 / i 3);;
- : D.t = 0.33333333333333333333333333333333
# (* round to decimal places: *)
  D.(round ~n:2 (of_int 22 / of_int 7));;
- : D.t = 3.14



dune build

Try in REPL:

dune utop


The test runner source is in test/ It parses and runs the test cases in test/data/. I am adding test case data files from the Python snapshot as I go.

Run current tests:

dune test

Note that, some of the tests don't make sense for the OCaml port and have thus been deleted. If you ever need to update to new versions of the test files, you can apply the changes as patches after re-dowloading the relevant *.decTest files:

git show 07074859567e936b8d170aba5ef58889a4d9d467 | git apply
git show ae0196377fb7a99db7f198f2fb242e6a2fe4541e | git apply

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Testing (93,345
Ocaml (6,956
Bigdecimal (218
Arbitrary Precision (191
Decimal (152