This commit is contained in:
swrup 2025-10-18 20:39:56 +02:00
parent d6e47b681f
commit 98d330a204

View file

@ -1,6 +1,7 @@
(* TODO
have safe amount arithmetic
make type private *)
make type private
use config currency option *)
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
Fixed-precision numbers with 8 decimal places.
- <Currency> must be at most 11 characters long
@ -21,7 +22,7 @@ let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e
let make ~sign ~currency ~value ~fraction =
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 in
let fraction_upper_bound = Int64.of_int 99_999_999 in
let fraction_upper_bound = Int64.of_int 100_000_000 in
let* () = if value < Int64.zero then Error "value is negative" else Ok () in
let* () =
if fraction < Int64.zero then Error "fraction is negative" else Ok ()
@ -31,7 +32,7 @@ let make ~sign ~currency ~value ~fraction =
else Ok ()
in
let* () =
if fraction > fraction_upper_bound then
if fraction >= fraction_upper_bound then
Error "fraction have more than 8 decimal digits"
else Ok ()
in
@ -46,8 +47,6 @@ let to_string =
in
let pp_currency ppf = function `Eur -> string ppf "EUR" in
fun ppf { sign; currency; value; fraction } ->
(* TODO amount
use config *)
pf ppf "%a%a:%Ld.%02Ld" (Fmt.option pp_sign) sign pp_currency currency
value fraction
in