This commit is contained in:
parent
d6e47b681f
commit
98d330a204
1 changed files with 4 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
(* TODO
|
(* TODO
|
||||||
have safe amount arithmetic
|
have safe amount arithmetic
|
||||||
make type private *)
|
make type private
|
||||||
|
use config currency option *)
|
||||||
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
|
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
|
||||||
Fixed-precision numbers with 8 decimal places.
|
Fixed-precision numbers with 8 decimal places.
|
||||||
- <Currency> must be at most 11 characters long
|
- <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 make ~sign ~currency ~value ~fraction =
|
||||||
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 in
|
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 value < Int64.zero then Error "value is negative" else Ok () in
|
||||||
let* () =
|
let* () =
|
||||||
if fraction < Int64.zero then Error "fraction is negative" else Ok ()
|
if fraction < Int64.zero then Error "fraction is negative" else Ok ()
|
||||||
|
|
@ -31,7 +32,7 @@ let make ~sign ~currency ~value ~fraction =
|
||||||
else Ok ()
|
else Ok ()
|
||||||
in
|
in
|
||||||
let* () =
|
let* () =
|
||||||
if fraction > fraction_upper_bound then
|
if fraction >= fraction_upper_bound then
|
||||||
Error "fraction have more than 8 decimal digits"
|
Error "fraction have more than 8 decimal digits"
|
||||||
else Ok ()
|
else Ok ()
|
||||||
in
|
in
|
||||||
|
|
@ -46,8 +47,6 @@ let to_string =
|
||||||
in
|
in
|
||||||
let pp_currency ppf = function `Eur -> string ppf "EUR" in
|
let pp_currency ppf = function `Eur -> string ppf "EUR" in
|
||||||
fun ppf { sign; currency; value; fraction } ->
|
fun ppf { sign; currency; value; fraction } ->
|
||||||
(* TODO amount
|
|
||||||
use config *)
|
|
||||||
pf ppf "%a%a:%Ld.%02Ld" (Fmt.option pp_sign) sign pp_currency currency
|
pf ppf "%a%a:%Ld.%02Ld" (Fmt.option pp_sign) sign pp_currency currency
|
||||||
value fraction
|
value fraction
|
||||||
in
|
in
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue