1pub trait IntoPoints {
3 fn into_points(self) -> u64;
4}
5
6impl IntoPoints for f64 {
7 #[inline(always)]
8 fn into_points(self) -> u64 {
9 points(self)
10 }
11}
12
13impl IntoPoints for f32 {
14 #[inline(always)]
15 fn into_points(self) -> u64 {
16 points(self as f64)
17 }
18}
19
20impl IntoPoints for u64 {
21 #[inline(always)]
22 fn into_points(self) -> u64 {
23 self
24 }
25}
26
27pub trait IntoPrice {
29 fn into_price(self) -> f64;
30}
31
32impl IntoPrice for u64 {
33 #[inline(always)]
34 fn into_price(self) -> f64 {
35 price(self)
36 }
37}
38
39impl IntoPrice for f64 {
40 fn into_price(self) -> f64 {
41 self
42 }
43}
44
45impl IntoPrice for f32 {
46 fn into_price(self) -> f64 {
47 self as f64
48 }
49}
50
51#[inline(always)]
53pub fn points(price: f64) -> u64 {
54 (price * 1e8).round() as u64
55}
56
57#[inline(always)]
59pub fn price(points: u64) -> f64 {
60 points as f64 / 1e8
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn price_points() {
69 assert_eq!(points(1.0), 100000000);
70 assert_eq!(points(2.55751896), 255751896);
71 assert_eq!(points(0.00049245), 49245);
72 assert_eq!(price(49245), 0.00049245);
73 assert_eq!(points(0.015399360), 1539936);
74 assert_eq!(price(1539936), 0.01539936);
75 assert_eq!(points(0.09744111), 9744111);
76 assert_eq!(price(9744111), 0.09744111);
77 assert_eq!(points(0.15399360), 15399360);
78 assert_eq!(price(15399360), 0.15399360);
79 assert_eq!(points(0.00000354), 354);
80 assert_eq!(price(354), 0.00000354);
81
82 assert_eq!(points(0.20425743), 20425743);
83 assert_eq!(price(20425743), 0.20425743);
84 assert_eq!(points(0.03300001), 03300001);
85 assert_eq!(price(03300001), 0.03300001);
86
87 assert_eq!(points(0.00010030), 10030);
89 assert_eq!(price(10030), 0.00010030);
90 assert_eq!(points(0.00001104), 1104);
92 assert_eq!(price(1104), 0.00001104);
93 assert_eq!(points(0.01497375), 1497375);
95 assert_eq!(price(1497375), 0.01497375);
96 }
97
98 #[test]
99 fn points_u64_max() {
100 let r = points(0.00015370);
101 let a = points(12e6);
102 assert!(r * a < ::std::u64::MAX);
103 }
104}