Skip to content

MetricParameter: allow text with 0 decimal places #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,62 @@
juce::String MetricParameter::toString (float value, int numDecimalPlaces)
{
const auto absValue = std::abs (value);
float valueInRange { value };
juce::String suffix {};

Check warning on line 45 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L44-L45

Added lines #L44 - L45 were not covered by tests

if (absValue < 1.0e-12f) // femto
{
return juce::String { value * 1.0e15f, numDecimalPlaces } + " f";
valueInRange = value * 1.0e15f;
suffix = " f";

Check warning on line 50 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L49-L50

Added lines #L49 - L50 were not covered by tests
}
if (absValue < 1.0e-9f) // pico
else if (absValue < 1.0e-9f) // pico

Check warning on line 52 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L52

Added line #L52 was not covered by tests
{
return juce::String { value * 1.0e12f, numDecimalPlaces } + " p";
valueInRange = value * 1.0e12f;
suffix = " p";

Check warning on line 55 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L54-L55

Added lines #L54 - L55 were not covered by tests
}
if (absValue < 1.0e-6f) // nano
else if (absValue < 1.0e-6f) // nano

Check warning on line 57 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L57

Added line #L57 was not covered by tests
{
return juce::String { value * 1.0e9f, numDecimalPlaces } + " n";
valueInRange = value * 1.0e9f;
suffix = " n";

Check warning on line 60 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L59-L60

Added lines #L59 - L60 were not covered by tests
}
if (absValue < 1.0e-3f) // micro
else if (absValue < 1.0e-3f) // micro

Check warning on line 62 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L62

Added line #L62 was not covered by tests
{
return juce::String { value * 1.0e6f, numDecimalPlaces } + " μ";
valueInRange = value * 1.0e6f;
suffix = juce::String::fromUTF8 (" μ");

Check warning on line 65 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L64-L65

Added lines #L64 - L65 were not covered by tests
}
if (absValue < 1.0f) // milli
else if (absValue < 1.0f) // milli

Check warning on line 67 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L67

Added line #L67 was not covered by tests
{
return juce::String { value * 1.0e3f, numDecimalPlaces } + " m";
valueInRange = value * 1.0e3f;
suffix = " m";

Check warning on line 70 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L69-L70

Added lines #L69 - L70 were not covered by tests
}
if (absValue < 1.0e3f) // units
else if (absValue < 1.0e3f) // units

Check warning on line 72 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L72

Added line #L72 was not covered by tests
{
return juce::String { value, numDecimalPlaces } + " ";
suffix = " ";

Check warning on line 74 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L74

Added line #L74 was not covered by tests
}
if (absValue < 1.0e6f) // kilo
else if (absValue < 1.0e6f) // kilo

Check warning on line 76 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L76

Added line #L76 was not covered by tests
{
return juce::String { value * 1.0e-3f, numDecimalPlaces } + " k";
valueInRange = value * 1.0e-3f;
suffix = " k";

Check warning on line 79 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L78-L79

Added lines #L78 - L79 were not covered by tests
}
if (absValue < 1.0e9f) // mega
else if (absValue < 1.0e9f) // mega

Check warning on line 81 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L81

Added line #L81 was not covered by tests
{
return juce::String { value * 1.0e-6f, numDecimalPlaces } + " M";
valueInRange = value * 1.0e-6f;
suffix = " M";

Check warning on line 84 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L83-L84

Added lines #L83 - L84 were not covered by tests
}
else // Giga
{
valueInRange = value * 1.0e-9f;
suffix = " G";

Check warning on line 89 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L88-L89

Added lines #L88 - L89 were not covered by tests
}

juce::String res;

Check warning on line 92 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L92

Added line #L92 was not covered by tests
if (numDecimalPlaces == 0)
res = juce::String { static_cast<int> (valueInRange) };

Check warning on line 94 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L94

Added line #L94 was not covered by tests
else
res = juce::String { valueInRange, numDecimalPlaces };
res += suffix;

Check warning on line 97 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L96-L97

Added lines #L96 - L97 were not covered by tests

// Giga
return juce::String { value * 1.0e-9f, numDecimalPlaces } + " G";
return res;

Check warning on line 99 in modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp

View check run for this annotation

Codecov / codecov/patch

modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp#L99

Added line #L99 was not covered by tests
}

float MetricParameter::fromString (const juce::String& str)
Expand Down
34 changes: 34 additions & 0 deletions tests/plugin_tests/chowdsp_parameters_test/MetricParameterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,38 @@ TEST_CASE ("Metric Parameter Test", "[plugin][parameters]")
REQUIRE (param.getCurrentValueAsText() == juce::String::fromUTF8 ("-1.00 mV"));
REQUIRE (getValueForText() == Catch::Approx { param.get() }.margin (1.0e-6));
}

SECTION ("Decimal Places")
{
static constexpr auto test_val = 1.54321f;
static constexpr auto test_val_milli = 1.54321e-3f;
std::array<std::tuple<int, std::string_view, std::string_view>, 5> tests {
std::tuple<int, std::string_view, std::string_view> { 4, "1.5432 V", "1.5432 mV" },
{ 3, "1.543 V", "1.543 mV" },
{ 2, "1.54 V", "1.54 mV" },
{ 1, "1.5 V", "1.5 mV" },
{ 0, "1 V", "1 mV" },
};

for (auto [num_decimals, test_str, test_str_milli] : tests)
{
chowdsp::MetricParameter param {
"param",
"Param",
juce::NormalisableRange { -2.0f, 2.0f },
0.0f,
juce::String::fromUTF8 ("V"),
num_decimals,
};

param.setParameterValue (test_val);
REQUIRE (param.getCurrentValueAsText() == chowdsp::toString (test_str));

if (num_decimals < 4)
{
param.setParameterValue (test_val_milli);
REQUIRE (param.getCurrentValueAsText() == chowdsp::toString (test_str_milli));
}
}
}
}