Skip to main content

fastnum/decimal/dec/impls/
fmt.rs

1use core::fmt::{self, Debug, Display, Formatter, LowerExp, UpperExp};
2
3use crate::decimal::{dec::format, utils, Decimal};
4
5impl<const N: usize> Display for Decimal<N> {
6    #[inline]
7    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
8        if self.is_nan() {
9            write!(f, "NaN")
10        } else if self.is_infinite() {
11            write!(f, "{}Inf", self.sign())
12        } else {
13            format::format(
14                self.digits.to_str_radix(10),
15                self.cb.get_scale(),
16                self.sign(),
17                f,
18            )
19        }
20    }
21}
22
23impl<const N: usize> LowerExp for Decimal<N> {
24    #[inline]
25    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
26        format::format_exponential(
27            self.digits.to_str_radix(10),
28            self.cb.get_scale(),
29            self.sign(),
30            f,
31            "e",
32        )
33    }
34}
35
36impl<const N: usize> UpperExp for Decimal<N> {
37    #[inline]
38    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39        format::format_exponential(
40            self.digits.to_str_radix(10),
41            self.cb.get_scale(),
42            self.sign(),
43            f,
44            "E",
45        )
46    }
47}
48
49impl<const N: usize> Debug for Decimal<N> {
50    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
51        utils::fmt::debug_print(&self.digits, &self.cb, Self::type_name(), f)
52    }
53}