1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-15 14:42:56 +08:00

Format percentage based on significant decimal digits

This commit is contained in:
cdwcgt 2023-05-27 23:35:09 +09:00
parent 06ea8efb16
commit 89c8ef3c9b
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2

View File

@ -89,14 +89,20 @@ namespace osu.Game.Graphics.UserInterface
double floatValue = value.ToDouble(NumberFormatInfo.InvariantInfo);
if (DisplayAsPercentage)
return floatValue.ToString("0%");
decimal decimalPrecision = normalise(CurrentNumber.Precision.ToDecimal(NumberFormatInfo.InvariantInfo), max_decimal_digits);
// Find the number of significant digits (we could have less than 5 after normalize())
int significantDigits = FormatUtils.FindPrecision(decimalPrecision);
if (DisplayAsPercentage)
{
if (significantDigits <= 2)
return floatValue.ToString("0%");
string format = "0." + new string('0', significantDigits - 2) + "%";
return floatValue.ToString(format);
}
string negativeSign = Math.Round(floatValue, significantDigits) < 0 ? "-" : string.Empty;
return $"{negativeSign}{Math.Abs(floatValue).ToString($"N{significantDigits}")}";