From c3b72fa5620096f8bfeaefc3e68b1313193e7506 Mon Sep 17 00:00:00 2001 From: juliancoffee Date: Sat, 28 Aug 2021 02:23:34 +0300 Subject: [PATCH] Rewrite multiplier_to_percentage to return f32 --- voxygen/src/hud/diary.rs | 9 +++++++-- voxygen/src/hud/mod.rs | 9 ++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/voxygen/src/hud/diary.rs b/voxygen/src/hud/diary.rs index 1c2ea47f9e..c2ab6b23d0 100644 --- a/voxygen/src/hud/diary.rs +++ b/voxygen/src/hud/diary.rs @@ -2773,8 +2773,13 @@ fn splice_multiplier<'loc>( desc: &str, multipler: f32, ) -> (&'loc str, Cow<'loc, str>) { - let percentage = hud::multiplier_to_percentage(multipler).unsigned_abs(); - splice_constant(i18n, title, desc, percentage) + let percentage = hud::multiplier_to_percentage(multipler).abs(); + + let title = i18n.get(title); + let desc = i18n.get(desc); + let desc = desc.replace("{boost}", &format!("{:.0}", percentage)); + + (title, Cow::Owned(desc)) } // Small helper function to get localized skill text. diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index c1c6f9fe91..b12cd57f5c 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -4140,12 +4140,15 @@ pub fn angle_of_attack_text( } /// Converts multiplier to percentage. +/// NOTE: floats are not the most precise type. /// /// # Examples /// ``` /// use veloren_voxygen::hud::multiplier_to_percentage; /// -/// assert_eq!(multiplier_to_percentage(1.05), 5); -/// assert_eq!(multiplier_to_percentage(0.85), -15); +/// let positive = multiplier_to_percentage(1.05); +/// assert!((positive - 5.0).abs() < 0.0001); +/// let negative = multiplier_to_percentage(0.85); +/// assert!((negative - (-15.0)).abs() < 0.0001); /// ``` -pub fn multiplier_to_percentage(value: f32) -> i32 { (value * 100.0 - 100.0).round() as i32 } +pub fn multiplier_to_percentage(value: f32) -> f32 { value * 100.0 - 100.0 }