1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:07:35 +00:00

Profiler: Display correctly rounded percentages as '#.##%'

This commit is contained in:
Jelle Raaijmakers 2022-08-31 16:36:33 +02:00 committed by Linus Groh
parent fcf86b07a5
commit a373542f4c
2 changed files with 12 additions and 6 deletions

View file

@ -111,10 +111,16 @@ GUI::Variant ProfileModel::data(GUI::ModelIndex const& index, GUI::ModelRole rol
return {}; return {};
} }
if (role == GUI::ModelRole::Display) { if (role == GUI::ModelRole::Display) {
auto round_percentages = [this](auto percentage) { auto round_percentages = [this](auto value) {
return roundf(static_cast<float>(percentage) / static_cast<float>(m_profile.filtered_event_indices().size()) auto percentage_full_precision = round_to<int>(
* percent_digits_rounding_constant) static_cast<float>(value)
* 100.0f / percent_digits_rounding_constant; * 100.f
/ static_cast<float>(m_profile.filtered_event_indices().size())
* percent_digits_rounding);
return String::formatted(
"{}.{:02}",
percentage_full_precision / percent_digits_rounding,
percentage_full_precision % percent_digits_rounding);
}; };
if (index.column() == Column::SampleCount) { if (index.column() == Column::SampleCount) {
if (m_profile.show_percentages()) if (m_profile.show_percentages())

View file

@ -14,8 +14,8 @@ namespace Profiler {
class Profile; class Profile;
// Number of digits after the decimal point for sample percentages. // Number of digits after the decimal point for sample percentages.
static constexpr int const number_of_percent_digits = 3; static constexpr int const number_of_percent_digits = 2;
static constexpr float const percent_digits_rounding_constant = AK::pow(10, number_of_percent_digits); static constexpr int const percent_digits_rounding = AK::pow(10, number_of_percent_digits);
class ProfileModel final : public GUI::Model { class ProfileModel final : public GUI::Model {
public: public: