2020-02-04 04:18:19 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-03-26 04:06:30 +00:00
|
|
|
using System;
|
2021-03-26 04:09:53 +00:00
|
|
|
using System.Globalization;
|
2020-12-19 14:02:56 +00:00
|
|
|
using Humanizer;
|
|
|
|
|
2020-02-04 04:17:23 +00:00
|
|
|
namespace osu.Game.Utils
|
|
|
|
{
|
2020-02-04 04:36:22 +00:00
|
|
|
public static class FormatUtils
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-02-04 04:17:23 +00:00
|
|
|
/// Turns the provided accuracy into a percentage with 2 decimal places.
|
|
|
|
/// </summary>
|
2021-03-26 04:09:53 +00:00
|
|
|
/// <param name="accuracy">The accuracy to be formatted.</param>
|
|
|
|
/// <param name="formatProvider">An optional format provider.</param>
|
2020-02-04 04:17:23 +00:00
|
|
|
/// <returns>formatted accuracy in percentage</returns>
|
2021-03-26 04:09:53 +00:00
|
|
|
public static string FormatAccuracy(this double accuracy, IFormatProvider formatProvider = null)
|
2021-03-25 08:34:29 +00:00
|
|
|
{
|
2021-03-26 04:06:30 +00:00
|
|
|
// 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;
|
2020-02-04 04:17:23 +00:00
|
|
|
|
2021-03-26 04:09:53 +00:00
|
|
|
return accuracy.ToString("0.00%", formatProvider ?? CultureInfo.CurrentCulture);
|
2021-03-25 08:34:29 +00:00
|
|
|
}
|
2020-12-19 14:02:56 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Formats the supplied rank/leaderboard position in a consistent, simplified way.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rank">The rank/position to be formatted.</param>
|
|
|
|
public static string FormatRank(this int rank) => rank.ToMetric(decimals: rank < 100_000 ? 1 : 0);
|
2020-02-04 04:36:22 +00:00
|
|
|
}
|
|
|
|
}
|