2020-02-04 12:18:19 +08: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 12:06:30 +08:00
|
|
|
using System;
|
2020-12-19 22:02:56 +08:00
|
|
|
using Humanizer;
|
2021-08-29 20:00:28 +08:00
|
|
|
using osu.Framework.Extensions.LocalisationExtensions;
|
2021-07-24 04:37:08 +08:00
|
|
|
using osu.Framework.Localisation;
|
2020-12-19 22:02:56 +08:00
|
|
|
|
2020-02-04 12:17:23 +08:00
|
|
|
namespace osu.Game.Utils
|
|
|
|
{
|
2020-02-04 12:36:22 +08:00
|
|
|
public static class FormatUtils
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-02-04 12:17:23 +08:00
|
|
|
/// Turns the provided accuracy into a percentage with 2 decimal places.
|
|
|
|
/// </summary>
|
2021-03-26 12:09:53 +08:00
|
|
|
/// <param name="accuracy">The accuracy to be formatted.</param>
|
2020-02-04 12:17:23 +08:00
|
|
|
/// <returns>formatted accuracy in percentage</returns>
|
2021-07-24 04:37:08 +08:00
|
|
|
public static LocalisableString FormatAccuracy(this double accuracy)
|
2021-03-25 16:34:29 +08:00
|
|
|
{
|
2021-03-26 12:06:30 +08: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 12:17:23 +08:00
|
|
|
|
2021-07-24 04:37:08 +08:00
|
|
|
return accuracy.ToLocalisableString("0.00%");
|
2021-03-25 16:34:29 +08:00
|
|
|
}
|
2020-12-19 22:02:56 +08: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);
|
2021-11-13 06:07:24 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Finds the number of digits after the decimal.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="d">The value to find the number of decimal digits for.</param>
|
|
|
|
/// <returns>The number decimal digits.</returns>
|
|
|
|
public static int FindPrecision(decimal d)
|
|
|
|
{
|
|
|
|
int precision = 0;
|
|
|
|
|
|
|
|
while (d != Math.Round(d))
|
|
|
|
{
|
|
|
|
d *= 10;
|
|
|
|
precision++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return precision;
|
|
|
|
}
|
2024-02-22 17:27:37 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Applies rounding to the given BPM value.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Double-rounding is applied intentionally (see https://github.com/ppy/osu/pull/18345#issue-1243311382 for rationale).
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="baseBpm">The base BPM to round.</param>
|
|
|
|
/// <param name="rate">Rate adjustment, if applicable.</param>
|
|
|
|
public static int RoundBPM(double baseBpm, double rate = 1) => (int)Math.Round(Math.Round(baseBpm) * rate);
|
2020-02-04 12:36:22 +08:00
|
|
|
}
|
|
|
|
}
|