From ed39b8e847f91f82ecbad62d08217486ce13ed1d Mon Sep 17 00:00:00 2001 From: ig Date: Thu, 17 Aug 2023 08:01:16 +0200 Subject: [PATCH] Fix Index out of range in .ToDisplayString() --- csharp/Lib/Units/Unit.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/csharp/Lib/Units/Unit.cs b/csharp/Lib/Units/Unit.cs index 5e5997191..efe0986f1 100644 --- a/csharp/Lib/Units/Unit.cs +++ b/csharp/Lib/Units/Unit.cs @@ -19,12 +19,12 @@ public abstract class Unit var i = 8; - while (a >= 10000) + while (a >= 10000 && i < MaxPrefix) { a /= 1000; i++; } - while (a < 10) + while (a < 10 && i > 0) { a *= 1000; i--; @@ -38,5 +38,7 @@ public abstract class Unit } private static readonly IReadOnlyList Prefix = new[] { "y", "z", "a", "f", "p", "n", "ยต", "m", "", "k", "M", "G", "T", "P", "E", "Y" }; + + private static Int32 MaxPrefix { get; } = Prefix.Count - 1; }