Skip to content

Commit

Permalink
format_number
Browse files Browse the repository at this point in the history
  • Loading branch information
epompeii committed Dec 26, 2024
1 parent aee296f commit c715eac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
42 changes: 4 additions & 38 deletions lib/bencher_comment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ fn value_cell(
baseline: Option<OrderedFloat<f64>>,
factor: OrderedFloat<f64>,
) -> String {
let mut cell = format_number((value / factor).into());
let mut cell = Units::format_number((value / factor).into());

if let Some(baseline) = baseline {
let percent = if value.is_normal() && baseline.is_normal() {
Expand All @@ -788,7 +788,7 @@ fn value_cell(
0.0.into()
};
let plus = if percent > 0.0.into() { "+" } else { "" };
let percent = format_number(percent.into());
let percent = Units::format_number(percent.into());
cell.push_str(&format!("<br />({plus}{percent}%)"));
}

Expand Down Expand Up @@ -861,8 +861,8 @@ fn limit_cell(
percent: OrderedFloat<f64>,
factor: OrderedFloat<f64>,
) -> String {
let mut cell = format_number((limit / factor).into());
let percent = format_number(percent.into());
let mut cell = Units::format_number((limit / factor).into());
let percent = Units::format_number(percent.into());
cell.push_str(&format!("<br />({percent}%)"));
cell
}
Expand All @@ -879,40 +879,6 @@ fn limit_cell(
html.push_str("</td>");
}

enum Position {
Whole(usize),
Point,
Decimal,
}

fn format_number(number: f64) -> String {
let mut number_str = String::new();
let mut position = Position::Decimal;
for c in format!("{:.2}", number.abs()).chars().rev() {
match position {
Position::Whole(place) => {
if place % 3 == 0 {
number_str.push(',');
}
position = Position::Whole(place + 1);
},
Position::Point => {
position = Position::Whole(1);
},
Position::Decimal => {
if c == '.' {
position = Position::Point;
}
},
}
number_str.push(c);
}
if number < 0.0 {
number_str.push('-');
}
number_str.chars().rev().collect()
}

#[derive(Clone, Copy)]
pub struct BoundaryLimits {
min: OrderedFloat<f64>,
Expand Down
38 changes: 38 additions & 0 deletions lib/bencher_valid/src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl Units {
pub fn scale_units(&self) -> String {
self.scale.units(self.units.as_ref())
}

pub fn format_number(number: f64) -> String {
format_number(number)
}
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -225,3 +229,37 @@ pub fn scale_factor(min: f64, units: &str) -> u64 {
pub fn scale_units(min: f64, units: &str) -> String {
Scale::new(min, units).units(units)
}

enum Position {
Whole(usize),
Point,
Decimal,
}

fn format_number(number: f64) -> String {
let mut number_str = String::new();
let mut position = Position::Decimal;
for c in format!("{:.2}", number.abs()).chars().rev() {
match position {
Position::Whole(place) => {
if place % 3 == 0 {
number_str.push(',');
}
position = Position::Whole(place + 1);
},
Position::Point => {
position = Position::Whole(1);
},
Position::Decimal => {
if c == '.' {
position = Position::Point;
}
},
}
number_str.push(c);
}
if number < 0.0 {
number_str.push('-');
}
number_str.chars().rev().collect()
}

0 comments on commit c715eac

Please sign in to comment.