Files
GladiatorIdle/Game/NumberFormat.cs
Michi a7134b2753 Gladiator Idle (MonoGame) – Spiel mit 3D-Gladiatoren
Nur was zum Bauen und Starten nötig ist: Mono/ (Spiel), Game/, Core/,
Languages/, Assets/ (Bilder, Clips, 3D-Figuren, Ton), build-release.bat.
Bauen: build-release.bat → Spiel\GladiatorIdle.exe

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:57:20 +01:00

55 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Globalization;
namespace GladiatorIdle
{
/// <summary>Große Zahlen kurz: 999 · 1,23K · 45,6M · 7,89B · 1,00T · danach 1,23e15.</summary>
internal static class NumberFormat
{
static readonly string[] Suffix = { "num.k", "num.m", "num.b", "num.t" };
public static CultureInfo Culture => L.Current switch
{
L.Lang.De => CultureInfo.GetCultureInfo("de-DE"),
L.Lang.Es => CultureInfo.GetCultureInfo("es-ES"),
_ => CultureInfo.GetCultureInfo("en-US")
};
public static string Format(double v)
{
var c = Culture;
if (double.IsNaN(v) || double.IsInfinity(v)) return "∞";
double a = Math.Abs(v);
if (a < 1000) return Math.Floor(v).ToString("N0", c);
int group = (int)Math.Floor(Math.Log10(a) / 3);
if (group > Suffix.Length) return v.ToString("0.00e0", c);
double scaled = v / Math.Pow(1000, group);
// 3 gültige Stellen: 1,23K · 12,3K · 123K
string num = Math.Abs(scaled) >= 100 ? scaled.ToString("0", c)
: Math.Abs(scaled) >= 10 ? scaled.ToString("0.0", c)
: scaled.ToString("0.00", c);
return num + L.t(Suffix[group - 1]);
}
/// Wie <see cref="Format"/>, aber unter 100 mit einer Nachkommastelle (XP pro Klick: 1,6).
public static string Small(double v) =>
Math.Abs(v) < 100 ? v.ToString("0.#", Culture) : Format(v);
/// Anteil 0..1 als Prozent ("12 %" / "12%" je Sprache).
public static string Percent(double frac) => string.Format(L.t("num.pct"), (frac * 100).ToString("0", Culture));
/// Faktor: ×2,1.
public static string Mult(double m) => "×" + m.ToString("0.0", Culture); // i18n-ok
/// Dauer kurz: 45 s · 12 min · 3 h 20 min.
public static string Duration(double seconds)
{
var t = TimeSpan.FromSeconds(Math.Max(0, seconds));
if (t.TotalMinutes < 1) return string.Format(L.t("time.s"), (int)t.TotalSeconds);
if (t.TotalHours < 1) return string.Format(L.t("time.m"), (int)t.TotalMinutes);
if (t.Minutes == 0) return string.Format(L.t("time.h"), (int)t.TotalHours);
return string.Format(L.t("time.hm"), (int)t.TotalHours, t.Minutes);
}
}
}