1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-14 04:12:55 +08:00

Add the ability to specify format provider (to make tests culture invariant)

This commit is contained in:
Dean Herbert 2021-03-26 13:09:53 +09:00
parent 6a7f926168
commit 4909eaf890
2 changed files with 7 additions and 4 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Globalization;
using NUnit.Framework; using NUnit.Framework;
using osu.Game.Utils; using osu.Game.Utils;
@ -19,7 +20,7 @@ namespace osu.Game.Tests.NonVisual
[TestCase(1, "100.00%")] [TestCase(1, "100.00%")]
public void TestAccuracyFormatting(double input, string expectedOutput) public void TestAccuracyFormatting(double input, string expectedOutput)
{ {
Assert.AreEqual(expectedOutput, input.FormatAccuracy()); Assert.AreEqual(expectedOutput, input.FormatAccuracy(CultureInfo.InvariantCulture));
} }
} }
} }

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Globalization;
using Humanizer; using Humanizer;
namespace osu.Game.Utils namespace osu.Game.Utils
@ -11,9 +12,10 @@ namespace osu.Game.Utils
/// <summary> /// <summary>
/// Turns the provided accuracy into a percentage with 2 decimal places. /// Turns the provided accuracy into a percentage with 2 decimal places.
/// </summary> /// </summary>
/// <param name="accuracy">The accuracy to be formatted</param> /// <param name="accuracy">The accuracy to be formatted.</param>
/// <param name="formatProvider">An optional format provider.</param>
/// <returns>formatted accuracy in percentage</returns> /// <returns>formatted accuracy in percentage</returns>
public static string FormatAccuracy(this double accuracy) public static string FormatAccuracy(this double accuracy, IFormatProvider formatProvider = null)
{ {
// for the sake of display purposes, we don't want to show a user a "rounded up" percentage to the next whole number. // for the sake of display purposes, we don't want to show a user a "rounded up" percentage to the next whole number.
// ie. a score which gets 89.99999% shouldn't ever show as 90%. // ie. a score which gets 89.99999% shouldn't ever show as 90%.
@ -21,7 +23,7 @@ namespace osu.Game.Utils
// percentile with a non-matching grade is confusing. // percentile with a non-matching grade is confusing.
accuracy = Math.Floor(accuracy * 10000) / 10000; accuracy = Math.Floor(accuracy * 10000) / 10000;
return $"{accuracy:0.00%}"; return accuracy.ToString("0.00%", formatProvider ?? CultureInfo.CurrentCulture);
} }
/// <summary> /// <summary>