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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::Signed;
use num_traits::cast::ToPrimitive;
use serde;
use std::fmt;
use std::result;

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RpNumber {
    // base digits
    pub digits: BigInt,
    // where the decimal point is
    pub decimal: usize,
}

impl RpNumber {
    fn multiple(&self) -> BigInt {
        let mut multiple: BigInt = 1.into();

        for _ in 0..self.decimal {
            let ten: BigInt = 10.into();
            multiple = multiple * ten;
        }

        multiple
    }

    pub fn to_u64(&self) -> Option<u64> {
        let m = self.multiple();

        self.digits.checked_div(&m).and_then(|r| r.to_u64())
    }

    pub fn to_u32(&self) -> Option<u32> {
        self.to_u64().map(|v| v as u32)
    }

    pub fn to_usize(&self) -> Option<usize> {
        self.to_u64().map(|v| v as usize)
    }

    pub fn to_f64(&self) -> Option<f64> {
        let multiple = self.multiple();
        let (base, decimal) = self.digits.div_mod_floor(&multiple);

        base.to_f64().and_then(|base| {
            decimal.to_f64().and_then(|decimal| {
                multiple
                    .to_f64()
                    .map(|multiple| base + (decimal / multiple))
            })
        })
    }
}

impl From<u32> for RpNumber {
    fn from(value: u32) -> RpNumber {
        RpNumber {
            digits: value.into(),
            decimal: 0usize,
        }
    }
}

impl From<i32> for RpNumber {
    fn from(value: i32) -> RpNumber {
        RpNumber {
            digits: value.into(),
            decimal: 0usize,
        }
    }
}

impl fmt::Debug for RpNumber {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "RpNumber({})", self)
    }
}

impl fmt::Display for RpNumber {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.decimal == 0 {
            return write!(f, "{}", self.digits);
        }

        let multiple = self.multiple();
        let (base, decimal) = self.digits.abs().div_mod_floor(&multiple);

        let decimal = format!("{}", decimal);

        // pad leading zeros if needed
        let decimal = if decimal.len() < self.decimal {
            let mut s = String::new();

            for _ in decimal.len()..self.decimal {
                s.push('0');
            }

            s.push_str(&decimal);
            s
        } else {
            decimal
        };

        if self.digits.is_negative() {
            write!(f, "-{}.{}", base, decimal)
        } else {
            write!(f, "{}.{}", base, decimal)
        }
    }
}

impl serde::Serialize for RpNumber {
    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let n = self.to_f64().unwrap();
        serializer.serialize_f64(n)
    }
}

#[cfg(test)]
mod test_numbers {
    use super::*;

    #[test]
    fn test_number() {
        let n = RpNumber {
            digits: 104321.into(),
            decimal: 2,
        };

        assert_eq!(Some(1043), n.to_u64());
        assert_eq!(Some(1043.21), n.to_f64());
    }

    #[test]
    fn test_negative() {
        let n = RpNumber {
            digits: (-104321).into(),
            decimal: 2,
        };

        assert_eq!(None, n.to_u64());
        assert_eq!(Some(-1043.21), n.to_f64());
    }

    #[test]
    fn test_display() {
        let n = RpNumber {
            digits: (104321).into(),
            decimal: 2,
        };

        assert_eq!("1043.21", format!("{}", n));

        let n2 = RpNumber {
            digits: (104321).into(),
            decimal: 0,
        };

        assert_eq!("104321", format!("{}", n2));

        let n3 = RpNumber {
            digits: (104321).into(),
            decimal: 10,
        };

        assert_eq!("0.0000104321", format!("{}", n3));
    }
}