use rug::{Complex, Float};
use std::fmt::Formatter;
use super::{float, FormatOptions, NumberFormat};
fn fmt_helper(f: &mut Formatter<'_>, n: &Float, options: FormatOptions) -> std::fmt::Result {
if n == &1 {
return write!(f, "i");
} else if n == &-1 {
return write!(f, "-i");
}
if options.number == NumberFormat::Scientific
|| options.number == NumberFormat::Auto && float::should_use_scientific(n)
{
write!(f, "(")?;
float::fmt_scientific(f, n, options)?;
write!(f, ")")?;
} else {
float::fmt(f, n, options)?;
}
write!(f, "i")
}
pub fn fmt(f: &mut Formatter<'_>, c: &Complex, options: FormatOptions) -> std::fmt::Result {
let (re, im) = (c.real(), c.imag());
match (re.is_zero(), im.is_zero()) {
(false, false) => {
float::fmt(f, re, options)?;
if im.is_sign_positive() {
write!(f, " + ")?;
fmt_helper(f, im, options)?;
} else {
write!(f, " - ")?;
fmt_helper(f, &im.as_neg(), options)?;
}
Ok(())
},
(false, true) => float::fmt(f, re, options),
(true, false) => fmt_helper(f, im, options),
(true, true) => write!(f, "0"),
}
}