1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-21 05:49:54 +08:00
Files
osu-lazer/osu.Game/Utils/FormatUtils.cs
T
Bartłomiej Dach 4fd2a488b7 Floor star rating to 2 decimal places rather than rounding
Rounding semi-regularly confuses users who aim for star rating pass / FC
medals and then feel they have been cheated out of a medal because they
passed an "X-star beatmap", but the actual star rating of the beatmap is
slightly under X.

The latest instance of this can be found at
https://osu.ppy.sh/community/forums/topics/2091333?n=2. The relevant
beatmap there is https://osu.ppy.sh/beatmapsets/2162554#osu/4746232,
whose raw star rating is 6.9976070253117344.

The other direction would be to fix the star rating medals instead, but
I think this is more reasonable given we already do similar things to
accuracy displays.
2025-06-13 09:11:06 +02:00

76 lines
3.2 KiB
C#

// 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 Humanizer;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Localisation;
namespace osu.Game.Utils
{
public static class FormatUtils
{
/// <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 LocalisableString FormatAccuracy(this double accuracy)
{
// 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;
return accuracy.ToLocalisableString("0.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);
/// <summary>
/// Formats the supplied star rating in a consistent, simplified way.
/// </summary>
/// <param name="starRating">The star rating to be formatted.</param>
public static LocalisableString FormatStarRating(this double starRating)
{
// for the sake of display purposes, we don't want to show a user a "rounded up" star rating to the next whole number.
// i.e. a beatmap which has a star rating of 6.9999* should never show as 7.00*.
// this matters for star rating medals which use hard cutoffs at whole numbers,
// which then confuses users when they beat a 6.9999* beatmap but don't get the 7-star medal.
starRating = Math.Floor(starRating * 100) / 100;
return starRating.ToLocalisableString("0.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;
}
/// <summary>
/// Applies rounding to the given BPM value.
/// </summary>
/// <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(baseBpm * rate);
}
}