diff --git a/flang/lib/Decimal/binary-to-decimal.cpp b/flang/lib/Decimal/binary-to-decimal.cpp --- a/flang/lib/Decimal/binary-to-decimal.cpp +++ b/flang/lib/Decimal/binary-to-decimal.cpp @@ -310,7 +310,6 @@ more.Next(); } number.Minimize(Big{less, rounding}, Big{more, rounding}); - } else { } return number.ConvertToDecimal(buffer, size, flags, digits); } diff --git a/flang/runtime/edit-output.cpp b/flang/runtime/edit-output.cpp --- a/flang/runtime/edit-output.cpp +++ b/flang/runtime/edit-output.cpp @@ -268,6 +268,7 @@ // Multiple conversions may be needed to get the right number of // effective rounded fractional digits. int extraDigits{0}; + bool canIncrease{true}; while (true) { decimal::ConversionToDecimalResult converted{ Convert(extraDigits + fracDigits, edit, flags)}; @@ -277,11 +278,12 @@ } int scale{IsZero() ? 1 : edit.modes.scale}; // kP int expo{converted.decimalExponent + scale}; - if (expo > extraDigits && extraDigits >= 0) { + if (expo > extraDigits && extraDigits >= 0 && canIncrease) { extraDigits = expo; if (!edit.digits.has_value()) { // F0 fracDigits = sizeof buffer_ - extraDigits - 2; // sign & NUL } + canIncrease = false; // only once continue; } else if (expo < extraDigits && extraDigits > -fracDigits) { extraDigits = std::max(expo, -fracDigits); diff --git a/flang/unittests/Runtime/NumericalFormatTest.cpp b/flang/unittests/Runtime/NumericalFormatTest.cpp --- a/flang/unittests/Runtime/NumericalFormatTest.cpp +++ b/flang/unittests/Runtime/NumericalFormatTest.cpp @@ -627,6 +627,20 @@ {"(F5.3,';')", -0.0025, "-.003;"}, {"(F5.3,';')", -0.00025, "-.000;"}, {"(F5.3,';')", -0.000025, "-.000;"}, + {"(F5.3,';')", 99.999, "*****;"}, + {"(F5.3,';')", 9.9999, "*****;"}, + {"(F5.3,';')", 0.99999, "1.000;"}, + {"(F5.3,';')", 0.099999, "0.100;"}, + {"(F5.3,';')", 0.0099999, "0.010;"}, + {"(F5.3,';')", 0.00099999, "0.001;"}, + {"(F5.3,';')", 0.000099999, "0.000;"}, + {"(F5.3,';')", -99.999, "*****;"}, + {"(F5.3,';')", -9.9999, "*****;"}, + {"(F5.3,';')", -0.99999, "*****;"}, + {"(F5.3,';')", -0.099999, "-.100;"}, + {"(F5.3,';')", -0.0099999, "-.010;"}, + {"(F5.3,';')", -0.00099999, "-.001;"}, + {"(F5.3,';')", -0.000099999, "-.000;"}, }; for (auto const &[format, value, expect] : individualTestCases) {