1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 19:22:54 +08:00

Merge pull request #12181 from peppy/fix-accuracy-formatting

This commit is contained in:
Bartłomiej Dach 2021-03-27 15:19:00 +01:00 committed by GitHub
commit b9b0623c83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 11 deletions

View File

@ -0,0 +1,26 @@
// 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.Utils;
namespace osu.Game.Tests.NonVisual
{
[TestFixture]
public class FormatUtilsTest
{
[TestCase(0, "0.00%")]
[TestCase(0.01, "1.00%")]
[TestCase(0.9899, "98.99%")]
[TestCase(0.989999, "98.99%")]
[TestCase(0.99, "99.00%")]
[TestCase(0.9999, "99.99%")]
[TestCase(0.999999, "99.99%")]
[TestCase(1, "100.00%")]
public void TestAccuracyFormatting(double input, string expectedOutput)
{
Assert.AreEqual(expectedOutput, input.FormatAccuracy(CultureInfo.InvariantCulture));
}
}
}

View File

@ -337,7 +337,7 @@ namespace osu.Game.Rulesets.Scoring
score.TotalScore = (long)Math.Round(GetStandardisedScore());
score.Combo = Combo.Value;
score.MaxCombo = HighestCombo.Value;
score.Accuracy = Math.Round(Accuracy.Value, 4);
score.Accuracy = Accuracy.Value;
score.Rank = Rank.Value;
score.Date = DateTimeOffset.Now;

View File

@ -30,7 +30,7 @@ namespace osu.Game.Scoring
public long TotalScore { get; set; }
[JsonProperty("accuracy")]
[Column(TypeName = "DECIMAL(1,4)")]
[Column(TypeName = "DECIMAL(1,4)")] // TODO: This data type is wrong (should contain more precision). But at the same time, we probably don't need to be storing this in the database.
public double Accuracy { get; set; }
[JsonIgnore]

View File

@ -42,7 +42,7 @@ namespace osu.Game.Users
public long RankedScore;
[JsonProperty(@"hit_accuracy")]
public decimal Accuracy;
public double Accuracy;
[JsonIgnore]
public string DisplayAccuracy => Accuracy.FormatAccuracy();

View File

@ -1,6 +1,8 @@
// 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;
using System.Globalization;
using Humanizer;
namespace osu.Game.Utils
@ -10,16 +12,19 @@ namespace osu.Game.Utils
/// <summary>
/// Turns the provided accuracy into a percentage with 2 decimal places.
/// </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>
public static string FormatAccuracy(this double accuracy) => $"{accuracy:0.00%}";
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.
// ie. a score which gets 89.99999% shouldn't ever show as 90%.
// the reasoning for this is that cutoffs for grade increases are at whole numbers and displaying the required
// percentile with a non-matching grade is confusing.
accuracy = Math.Floor(accuracy * 10000) / 10000;
/// <summary>
/// Turns the provided accuracy into a percentage with 2 decimal places.
/// </summary>
/// <param name="accuracy">The accuracy to be formatted</param>
/// <returns>formatted accuracy in percentage</returns>
public static string FormatAccuracy(this decimal accuracy) => $"{accuracy:0.00}%";
return accuracy.ToString("0.00%", formatProvider ?? CultureInfo.CurrentCulture);
}
/// <summary>
/// Formats the supplied rank/leaderboard position in a consistent, simplified way.