1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
type t = int64
let of_int amount = Int64.(mul 1_000_000L (of_int amount))
let of_mutez_int = Int64.of_int
let of_mutez_int64 t = t
let zero = 0L
let one = of_int 1
let mutez_int64 t = t
let to_string amount =
let mult_int = 1_000_000L in
let rec left amount =
let d, r = (Int64.(div amount 1000L), Int64.(rem amount 1000L)) in
if d > 0L then Format.asprintf "%s%03Ld" (left d) r
else Format.asprintf "%Ld" r
in
let right amount =
let triplet v =
if v mod 10 > 0 then Format.asprintf "%03d" v
else if v mod 100 > 0 then Format.asprintf "%02d" (v / 10)
else Format.asprintf "%d" (v / 100)
in
let hi, lo = (amount / 1000, amount mod 1000) in
if lo = 0 then Format.asprintf "%s" (triplet hi)
else Format.asprintf "%03d%s" hi (triplet lo)
in
let ints, decs =
(Int64.(div amount mult_int), Int64.(to_int (rem amount mult_int)))
in
if decs > 0 then Format.asprintf "%s.%s" (left ints) (right decs)
else left ints
let to_float amount = Float.mul (Int64.to_float amount) 0.000_001
let to_mutez amount = Int64.to_int amount
let ( + ) = Int64.add
let ( - ) = Int64.sub
let parse_floating tez_string =
let re = rex "(\\d+)\\.?(\\d*)" in
let fail () = Test.fail "Invalid tez value: '%s'." tez_string in
let parse_int s =
match int_of_string_opt s with None -> fail () | Some i -> i
in
let integral, decimal =
match tez_string =~** re with None -> fail () | Some (i, d) -> (i, d)
in
let integral = parse_int integral in
let decimal =
match String.length decimal with
| 0 -> 0
| 1 -> 100_000 * parse_int decimal
| 2 -> 10_000 * parse_int decimal
| 3 -> 1_000 * parse_int decimal
| 4 -> 100 * parse_int decimal
| 5 -> 10 * parse_int decimal
| 6 -> parse_int decimal
| _ -> fail ()
in
of_int integral + of_mutez_int decimal
let typ =
Check.comparable
(fun fmt t -> Format.fprintf fmt "%s" (to_string t))
(fun a b -> Int.compare (to_mutez a) (to_mutez b))