ccf_gpui_widgets/utils/
mod.rs1pub mod color;
4pub mod path;
5
6pub use color::{Rgb, Rgba, Hsl, Hsv, named_color_to_rgb, parse_color, parse_color_alpha};
7pub use color::{luminance, is_dark, lighten, darken, mix};
8pub use path::{parse_path, expand_tilde, PathInfo};
9
10pub fn format_display_value(value: f64, precision: Option<usize>) -> String {
15 match precision {
16 Some(p) => {
17 let multiplier = 10_f64.powi(p as i32);
18 let rounded = (value * multiplier).round() / multiplier;
19 format!("{:.prec$}", rounded, prec = p)
20 }
21 None => {
22 if value.fract() == 0.0 {
23 format!("{:.0}", value)
24 } else {
25 format!("{}", value)
26 .trim_end_matches('0')
27 .trim_end_matches('.')
28 .to_string()
29 }
30 }
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 #[test]
39 fn test_format_display_value_with_precision() {
40 assert_eq!(format_display_value(1.23456, Some(2)), "1.23");
41 assert_eq!(format_display_value(1.23956, Some(2)), "1.24");
42 assert_eq!(format_display_value(5.0, Some(2)), "5.00");
43 assert_eq!(format_display_value(0.1, Some(3)), "0.100");
44 }
45
46 #[test]
47 fn test_format_display_value_without_precision() {
48 assert_eq!(format_display_value(42.0, None), "42");
49 assert_eq!(format_display_value(1.23, None), "1.23");
50 assert_eq!(format_display_value(3.10, None), "3.1");
51 assert_eq!(format_display_value(3.100, None), "3.1");
52 assert_eq!(format_display_value(0.5, None), "0.5");
53 }
54}