use crate::enums::page::cdrUnit;
fn to_inch_factor(u: cdrUnit) -> Option<f64> {
use cdrUnit::*;
Some(match u {
Inch => 1.0,
Millimeter => 1.0 / 25.4,
Centimeter => 1.0 / 2.54,
Point => 1.0 / 72.0,
Pica => 1.0 / 6.0,
Meter => 1.0 / 0.0254,
Kilometer => 1.0 / 0.0000254,
Foot => 12.0,
Yard => 36.0,
Mile => 63360.0,
_ => return None,
})
}
pub fn convert(value: f64, from: cdrUnit, to: cdrUnit) -> Option<f64> {
let f = to_inch_factor(from)?;
let t = to_inch_factor(to)?;
let raw = value * f / t;
Some(round_to_significant(raw, 12))
}
fn round_to_significant(x: f64, digits: i32) -> f64 {
if x == 0.0 || !x.is_finite() {
return x;
}
let magnitude = x.abs().log10().floor() as i32;
let factor = 10f64.powi(digits - magnitude - 1);
(x * factor).round() / factor
}
pub fn convert_by_i32(value: f64, from: i32, to: i32) -> Option<f64> {
let f = cdrUnit::from_i32(from)?;
let t = cdrUnit::from_i32(to)?;
convert(value, f, t)
}