fix unit conversion with negative distances

This commit is contained in:
yuni 2024-04-08 03:15:54 +02:00
parent e95f019f3f
commit c9f7422af1

View file

@ -84,19 +84,20 @@ pub fn ring_density(radius: f32) -> f32 {
} }
pub fn readable_distance(distance: f64) -> String { pub fn readable_distance(distance: f64) -> String {
if distance > LIGHTYEAR2METER * 0.001 { let abs_distance = distance.abs();
if abs_distance > LIGHTYEAR2METER * 0.001 {
let lightyears = distance / LIGHTYEAR2METER; let lightyears = distance / LIGHTYEAR2METER;
return format!("{lightyears:.3}ly"); return format!("{lightyears:.3}ly");
} }
if distance >= 1.0e10 { if abs_distance >= 1.0e10 {
let gigameters = distance * 1.0e-9; let gigameters = distance * 1.0e-9;
return format!("{gigameters:.1}Gm"); return format!("{gigameters:.1}Gm");
} }
if distance >= 1.0e7 { if abs_distance >= 1.0e7 {
let megameters = distance * 1.0e-6; let megameters = distance * 1.0e-6;
return format!("{megameters:.1}Mm"); return format!("{megameters:.1}Mm");
} }
if distance >= 1.0e4 { if abs_distance >= 1.0e4 {
let kilometers = distance * 1.0e-3; let kilometers = distance * 1.0e-3;
return format!("{kilometers:.1}km"); return format!("{kilometers:.1}km");
} }