#![no_std]
use ufmt::{uDisplay,Formatter,uWrite,uwrite};
#[allow(non_camel_case_types)]
#[derive(Debug,Clone,Copy)]
pub enum uFmt_f32 {
Zero(f32),
One(f32),
Two(f32),
Three(f32),
Four(f32),
Five(f32),
}
impl uDisplay for uFmt_f32 {
fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
where
W: uWrite + ?Sized,
{
use uFmt_f32::*;
let (number,decimals) = match self {
Zero(x) => float_to_int_f32(*x,0),
One(x) => float_to_int_f32(*x,1),
Two(x) => float_to_int_f32(*x,2),
Three(x) => float_to_int_f32(*x,3),
Four(x) => float_to_int_f32(*x,4),
Five(x) => float_to_int_f32(*x,5),
};
uwrite!(f, "{}", number)?;
match self {
One(_) => uwrite!(f, ".{}", decimals),
Two(_) if decimals >= 10 => uwrite!(f, ".{}", decimals),
Two(_) if decimals < 10 => uwrite!(f, ".0{}", decimals),
Three(_) if decimals >= 100 => uwrite!(f, ".{}", decimals),
Three(_) if decimals < 100 && decimals >= 10 => uwrite!(f, ".0{}", decimals),
Three(_) if decimals < 10 => uwrite!(f, ".00{}", decimals),
Four(_) if decimals >= 1000 => uwrite!(f, ".{}", decimals),
Four(_) if decimals < 1000 && decimals >= 100 => uwrite!(f, ".0{}", decimals),
Four(_) if decimals < 100 && decimals >= 10 => uwrite!(f, ".00{}", decimals),
Four(_) if decimals < 10 => uwrite!(f, ".000{}", decimals),
Five(_) if decimals >= 10000 => uwrite!(f, ".{}", decimals),
Five(_) if decimals < 10000 && decimals >= 1000 => uwrite!(f, ".0{}", decimals),
Five(_) if decimals < 1000 && decimals >= 100 => uwrite!(f, ".00{}", decimals),
Five(_) if decimals < 100 && decimals >= 10 => uwrite!(f, ".000{}", decimals),
Five(_) if decimals < 10 => uwrite!(f, ".0000{}", decimals),
_ => Ok(())
}
}
}
#[allow(non_camel_case_types)]
#[derive(Debug,Clone,Copy)]
pub enum uFmt_f64 {
Zero(f64),
One(f64),
Two(f64),
Three(f64),
Four(f64),
Five(f64),
}
impl uDisplay for uFmt_f64 {
fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
where
W: uWrite + ?Sized,
{
use uFmt_f64::*;
let (number,decimals) = match self {
Zero(x) => float_to_int_f64(*x,0),
One(x) => float_to_int_f64(*x,1),
Two(x) => float_to_int_f64(*x,2),
Three(x) => float_to_int_f64(*x,3),
Four(x) => float_to_int_f64(*x,4),
Five(x) => float_to_int_f64(*x,5),
};
uwrite!(f, "{}", number)?;
match self {
One(_) => uwrite!(f, ".{}", decimals),
Two(_) if decimals >= 10 => uwrite!(f, ".{}", decimals),
Two(_) if decimals < 10 => uwrite!(f, ".0{}", decimals),
Three(_) if decimals >= 100 => uwrite!(f, ".{}", decimals),
Three(_) if decimals < 100 && decimals >= 10 => uwrite!(f, ".0{}", decimals),
Three(_) if decimals < 10 => uwrite!(f, ".00{}", decimals),
Four(_) if decimals >= 1000 => uwrite!(f, ".{}", decimals),
Four(_) if decimals < 1000 && decimals >= 100 => uwrite!(f, ".0{}", decimals),
Four(_) if decimals < 100 && decimals >= 10 => uwrite!(f, ".00{}", decimals),
Four(_) if decimals < 10 => uwrite!(f, ".000{}", decimals),
Five(_) if decimals >= 10000 => uwrite!(f, ".{}", decimals),
Five(_) if decimals < 10000 && decimals >= 1000 => uwrite!(f, ".0{}", decimals),
Five(_) if decimals < 1000 && decimals >= 100 => uwrite!(f, ".00{}", decimals),
Five(_) if decimals < 100 && decimals >= 10 => uwrite!(f, ".000{}", decimals),
Five(_) if decimals < 10 => uwrite!(f, ".0000{}", decimals),
_ => Ok(())
}
}
}
#[cfg(feature = "maths")]
fn float_to_int_f32(orginal: f32,precision: u8) -> (i32,u32) {
#[allow(unused_imports)]
use micromath::F32Ext;
let prec = match precision {
1 => 10.0,
2 => 100.0,
3 => 1000.0,
4 => 10000.0,
5 => 100000.0,
_ => 0.0,
};
let base = orginal.trunc() as i32;
let decimal = (orginal.fract() * prec) as u32;
(base,decimal)
}
#[cfg(not(feature = "maths"))]
fn float_to_int_f32(orginal: f32,precision: u8) -> (i32,u32) {
let prec = match precision {
1 => 10.0,
2 => 100.0,
3 => 1000.0,
4 => 10000.0,
5 => 100000.0,
_ => 0.0,
};
let base = orginal as i32;
let decimal = ((orginal - (base as f32)) * prec) as u32;
(base,decimal)
}
fn float_to_int_f64(orginal: f64,precision: u8) -> (i32,u32) {
let prec = match precision {
1 => 10.0,
2 => 100.0,
3 => 1000.0,
4 => 10000.0,
5 => 100000.0,
_ => 0.0,
};
let base = orginal as i32;
let decimal = ((orginal - (base as f64)) * prec) as u32;
(base,decimal)
}