diff --git a/modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp b/modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp index e179f789..310d98f2 100644 --- a/modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp +++ b/modules/plugin/chowdsp_parameters/ParamUtils/chowdsp_MetricParameter.cpp @@ -41,41 +41,62 @@ MetricParameter::MetricParameter ( juce::String MetricParameter::toString (float value, int numDecimalPlaces) { const auto absValue = std::abs (value); + float valueInRange { value }; + juce::String suffix {}; + if (absValue < 1.0e-12f) // femto { - return juce::String { value * 1.0e15f, numDecimalPlaces } + " f"; + valueInRange = value * 1.0e15f; + suffix = " f"; } - if (absValue < 1.0e-9f) // pico + else if (absValue < 1.0e-9f) // pico { - return juce::String { value * 1.0e12f, numDecimalPlaces } + " p"; + valueInRange = value * 1.0e12f; + suffix = " p"; } - if (absValue < 1.0e-6f) // nano + else if (absValue < 1.0e-6f) // nano { - return juce::String { value * 1.0e9f, numDecimalPlaces } + " n"; + valueInRange = value * 1.0e9f; + suffix = " n"; } - if (absValue < 1.0e-3f) // micro + else if (absValue < 1.0e-3f) // micro { - return juce::String { value * 1.0e6f, numDecimalPlaces } + " μ"; + valueInRange = value * 1.0e6f; + suffix = juce::String::fromUTF8 (" μ"); } - if (absValue < 1.0f) // milli + else if (absValue < 1.0f) // milli { - return juce::String { value * 1.0e3f, numDecimalPlaces } + " m"; + valueInRange = value * 1.0e3f; + suffix = " m"; } - if (absValue < 1.0e3f) // units + else if (absValue < 1.0e3f) // units { - return juce::String { value, numDecimalPlaces } + " "; + suffix = " "; } - if (absValue < 1.0e6f) // kilo + else if (absValue < 1.0e6f) // kilo { - return juce::String { value * 1.0e-3f, numDecimalPlaces } + " k"; + valueInRange = value * 1.0e-3f; + suffix = " k"; } - if (absValue < 1.0e9f) // mega + else if (absValue < 1.0e9f) // mega { - return juce::String { value * 1.0e-6f, numDecimalPlaces } + " M"; + valueInRange = value * 1.0e-6f; + suffix = " M"; } + else // Giga + { + valueInRange = value * 1.0e-9f; + suffix = " G"; + } + + juce::String res; + if (numDecimalPlaces == 0) + res = juce::String { static_cast (valueInRange) }; + else + res = juce::String { valueInRange, numDecimalPlaces }; + res += suffix; - // Giga - return juce::String { value * 1.0e-9f, numDecimalPlaces } + " G"; + return res; } float MetricParameter::fromString (const juce::String& str) diff --git a/tests/plugin_tests/chowdsp_parameters_test/MetricParameterTest.cpp b/tests/plugin_tests/chowdsp_parameters_test/MetricParameterTest.cpp index 0b0378e3..4412f44c 100644 --- a/tests/plugin_tests/chowdsp_parameters_test/MetricParameterTest.cpp +++ b/tests/plugin_tests/chowdsp_parameters_test/MetricParameterTest.cpp @@ -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, 5> tests { + std::tuple { 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)); + } + } + } }