use crate::{ComparableFloat, ComparableFloatRef, Float};
use alloc::string::ToString;
use core::fmt::{Formatter, Result, Write};
use malachite_base::strings::typst::ToTypst;
impl ToTypst for Float {
fn fmt_typst(&self, f: &mut Formatter) -> Result {
if self.is_nan() {
return f.write_str("\"NaN\"");
} else if self.is_infinite() {
return f.write_str(if self.is_sign_positive() {
"infinity"
} else {
"-infinity"
});
}
let s = self.to_string();
let Some(e_index) = s.find('e') else {
return f.write_str(&s);
};
let (mantissa, exponent) = s.split_at(e_index);
let exponent = &exponent[1..];
f.write_str(mantissa)?;
f.write_str(" times 10^")?;
f.write_char('(')?;
f.write_str(exponent)?;
f.write_char(')')
}
}
impl ToTypst for ComparableFloat {
#[inline]
fn fmt_typst(&self, f: &mut Formatter) -> Result {
self.0.fmt_typst(f)
}
}
impl ToTypst for ComparableFloatRef<'_> {
#[inline]
fn fmt_typst(&self, f: &mut Formatter) -> Result {
self.0.fmt_typst(f)
}
}