1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-29 02:29:54 +08:00

Always use current culture in ToStandardFormattedString()

This commit is contained in:
Bartłomiej Dach
2025-10-24 08:26:01 +02:00
Unverified
parent a29a5ab7e6
commit ae7ba034a7
6 changed files with 17 additions and 30 deletions
@@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Globalization;
using osu.Framework.Bindables;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
@@ -53,7 +52,7 @@ namespace osu.Game.Rulesets.Catch.Mods
return string.Empty;
string format(string acronym, DifficultyBindable bindable)
=> $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(1, cultureInfo: CultureInfo.InvariantCulture)}";
=> $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(1)}";
}
}
@@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Globalization;
using osu.Framework.Bindables;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
@@ -53,7 +52,7 @@ namespace osu.Game.Rulesets.Osu.Mods
return string.Empty;
string format(string acronym, DifficultyBindable bindable)
=> $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(1, cultureInfo: CultureInfo.InvariantCulture)}";
=> $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(1)}";
}
}
@@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Globalization;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
@@ -36,7 +35,7 @@ namespace osu.Game.Rulesets.Taiko.Mods
return string.Empty;
string format(string acronym, DifficultyBindable bindable, int digits)
=> $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(digits, cultureInfo: CultureInfo.InvariantCulture)}";
=> $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(digits)}";
}
}
@@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Globalization;
using NUnit.Framework;
using osu.Game.Extensions;
@@ -18,9 +17,10 @@ namespace osu.Game.Tests.Extensions
[TestCase(0, true, 0, ExpectedResult = "0%")]
[TestCase(1, true, 0, ExpectedResult = "1%")]
[TestCase(50, true, 0, ExpectedResult = "50%")]
[SetCulture("")] // invariant culture
public string TestInteger(int input, bool percent, int decimalDigits)
{
return input.ToStandardFormattedString(decimalDigits, percent, CultureInfo.InvariantCulture);
return input.ToStandardFormattedString(decimalDigits, percent);
}
[TestCase(-1, false, 0, ExpectedResult = "-1")]
@@ -40,17 +40,8 @@ namespace osu.Game.Tests.Extensions
[TestCase(0.48333, true, 2, ExpectedResult = "48%")]
[TestCase(0.48333, true, 4, ExpectedResult = "48.33%")]
[TestCase(1, true, 0, ExpectedResult = "100%")]
[SetCulture("")] // invariant culture
public string TestDouble(double input, bool percent, int decimalDigits)
{
return input.ToStandardFormattedString(decimalDigits, percent, CultureInfo.InvariantCulture);
}
[Test]
[SetCulture("fr-FR")]
[TestCase(0.4, true, 2, ExpectedResult = "40%")]
[TestCase(1e-6, false, 6, ExpectedResult = "0,000001")]
[TestCase(0.48333, true, 4, ExpectedResult = "48,33%")]
public string TestCultureSensitivityWhenNoneSpecified(double input, bool percent, int decimalDigits)
{
return input.ToStandardFormattedString(decimalDigits, percent);
}
@@ -58,11 +49,11 @@ namespace osu.Game.Tests.Extensions
[Test]
[SetCulture("fr-FR")]
[TestCase(0.4, true, 2, ExpectedResult = "40%")]
[TestCase(1e-6, false, 6, ExpectedResult = "0.000001")]
[TestCase(0.48333, true, 4, ExpectedResult = "48.33%")]
public string TestCultureInsensitivityWhenInvariantSpecified(double input, bool percent, int decimalDigits)
[TestCase(1e-6, false, 6, ExpectedResult = "0,000001")]
[TestCase(0.48333, true, 4, ExpectedResult = "48,33%")]
public string TestCultureSensitivity(double input, bool percent, int decimalDigits)
{
return input.ToStandardFormattedString(decimalDigits, percent, CultureInfo.InvariantCulture);
return input.ToStandardFormattedString(decimalDigits, percent);
}
}
}
@@ -13,15 +13,15 @@ namespace osu.Game.Extensions
/// <summary>
/// For a given numeric type, return a formatted string in the standard format we use for display everywhere.
/// </summary>
/// <remarks>
/// Number formatting will abide by <see cref="CultureInfo.CurrentCulture"/>.
/// </remarks>
/// <param name="value">The numeric value.</param>
/// <param name="maxDecimalDigits">The maximum number of decimals to be considered in the original value.</param>
/// <param name="asPercentage">Whether the output should be a percentage. For integer types, 0-100 is mapped to 0-100%; for other types 0-1 is mapped to 0-100%.</param>
/// <param name="cultureInfo">The culture to use when formatting the value. Defaults to <see cref="CultureInfo.CurrentCulture"/> if not specified.</param>
/// <returns>The formatted output.</returns>
public static string ToStandardFormattedString<T>(this T value, int maxDecimalDigits, bool asPercentage = false, CultureInfo? cultureInfo = null) where T : struct, INumber<T>, IMinMaxValue<T>
public static string ToStandardFormattedString<T>(this T value, int maxDecimalDigits, bool asPercentage = false) where T : struct, INumber<T>, IMinMaxValue<T>
{
cultureInfo ??= CultureInfo.CurrentCulture;
double floatValue = double.CreateTruncating(value);
decimal decimalPrecision = normalise(decimal.CreateTruncating(value), maxDecimalDigits);
@@ -34,12 +34,12 @@ namespace osu.Game.Extensions
if (value is int)
floatValue /= 100;
return floatValue.ToString($@"0.{new string('0', Math.Max(0, significantDigits - 2))}%", cultureInfo);
return floatValue.ToString($@"0.{new string('0', Math.Max(0, significantDigits - 2))}%", CultureInfo.CurrentCulture);
}
string negativeSign = Math.Round(floatValue, significantDigits) < 0 ? "-" : string.Empty;
return $"{negativeSign}{Math.Abs(floatValue).ToString($"N{significantDigits}", cultureInfo)}";
return $"{negativeSign}{Math.Abs(floatValue).ToString($"N{significantDigits}", CultureInfo.CurrentCulture)}";
}
/// <summary>
@@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
@@ -82,7 +81,7 @@ namespace osu.Game.Rulesets.Mods
return string.Empty;
string format(string acronym, DifficultyBindable bindable) => $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(1, cultureInfo: CultureInfo.InvariantCulture)}";
string format(string acronym, DifficultyBindable bindable) => $"{acronym}{bindable.Value!.Value.ToStandardFormattedString(1)}";
}
}