mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-09 23:34:20 +00:00
String: Fix a bug in calculating the number of significant digits for exact powers of 10
This commit is contained in:
parent
b95ede49ff
commit
5b4fca7bd0
2 changed files with 18 additions and 5 deletions
|
|
@ -2878,6 +2878,20 @@ public:
|
|||
expectEquals (String::toDecimalStringWithSignificantFigures (2.8647, 6), String ("2.86470"));
|
||||
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures (-0.0000000000019, 1), String ("-0.000000000002"));
|
||||
|
||||
// Powers of 10
|
||||
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 0.001, 7), String ( "0.001000000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 0.01, 7), String ( "0.01000000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 0.1, 7), String ( "0.1000000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 1, 7), String ( "1.000000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 10, 7), String ( "10.00000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 100, 7), String ( "100.0000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 1000, 7), String ( "1000.000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 10000, 7), String ( "10000.00"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 100000, 7), String ( "100000.0"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures ( 1000000, 7), String ( "1000000"));
|
||||
expectEquals (String::toDecimalStringWithSignificantFigures (10000000, 7), String ("10000000"));
|
||||
}
|
||||
|
||||
beginTest ("Float trimming");
|
||||
|
|
|
|||
|
|
@ -1159,11 +1159,10 @@ public:
|
|||
return "0";
|
||||
}
|
||||
|
||||
auto numDigitsBeforePoint = (int) std::ceil (std::log10 (number < 0 ? -number : number));
|
||||
|
||||
auto shift = numberOfSignificantFigures - numDigitsBeforePoint;
|
||||
auto factor = std::pow (10.0, shift);
|
||||
auto rounded = std::round (number * factor) / factor;
|
||||
const auto numDigitsBeforePoint = (int) std::floor (std::log10 (std::abs (number)) + DecimalType (1));
|
||||
const auto shift = numberOfSignificantFigures - numDigitsBeforePoint;
|
||||
const auto factor = std::pow (10.0, shift);
|
||||
const auto rounded = std::round (number * factor) / factor;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::fixed << std::setprecision (std::max (shift, 0)) << rounded;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue