diff --git a/csharp/App/SaliMax/src/Flow.cs b/csharp/App/SaliMax/src/Flow.cs index ddebc25fb..8b74b48c5 100644 --- a/csharp/App/SaliMax/src/Flow.cs +++ b/csharp/App/SaliMax/src/Flow.cs @@ -14,7 +14,7 @@ public static class Flow public static TextBlock Horizontal(Unit amount, Int32 width = 10) { - var label = amount.ToStringRounded(); + var label = amount.ToDisplayString(); var arrowChar = amount.Value < 0 ? LeftArrowChar : RightArrowChar; var arrow = Enumerable.Repeat(arrowChar, width).Join(); @@ -26,7 +26,7 @@ public static class Flow [SuppressMessage("ReSharper", "CoVariantArrayConversion")] public static TextBlock Vertical(Unit amount, Int32 height = 4) { - var label = amount.ToStringRounded(); + var label = amount.ToDisplayString(); var arrowChar = amount.Value < 0 ? UpArrowChar : DownArrowChar; var halfArrow = Enumerable.Repeat(arrowChar, height/2); diff --git a/csharp/App/SaliMax/src/System/StateMachine.cs b/csharp/App/SaliMax/src/System/StateMachine.cs index 75abc1ae8..a22afb483 100644 --- a/csharp/App/SaliMax/src/System/StateMachine.cs +++ b/csharp/App/SaliMax/src/System/StateMachine.cs @@ -1,7 +1,9 @@ namespace InnovEnergy.App.SaliMax.System; -public class StateMachine +public record StateMachine { - public String Message { get; set; } = "Panic: Unknown State!"; - public Int32 State { get; set; } = 100; + public required String Message { get; set; } // TODO: init only + public required Int32 State { get; set; } // TODO: init only + + public static StateMachine Default { get; } = new StateMachine { State = 100, Message = "Unknown State" }; } \ No newline at end of file diff --git a/csharp/Lib/Units/Unit.cs b/csharp/Lib/Units/Unit.cs index e007f7f66..5e5997191 100644 --- a/csharp/Lib/Units/Unit.cs +++ b/csharp/Lib/Units/Unit.cs @@ -8,5 +8,35 @@ public abstract class Unit public Double Value { get; } public override String ToString() => $"{Value} {Symbol}"; - public String ToStringRounded() => $"{Math.Round(Value,3)} {Symbol}"; -} \ No newline at end of file + + public String ToDisplayString() + { + if (Value == 0) + return $"0 {Symbol}"; + + var a = Math.Abs(Value); + var s = Math.Sign(Value); + + var i = 8; + + while (a >= 10000) + { + a /= 1000; + i++; + } + while (a < 10) + { + a *= 1000; + i--; + } + + var r = a < 100 + ? Math.Floor(a * 10) / 10 + : Math.Floor(a); + + return $"{r * s} {Prefix[i]}{Symbol}"; + } + + private static readonly IReadOnlyList Prefix = new[] { "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Y" }; +} + diff --git a/csharp/Lib/Units/Units.cs b/csharp/Lib/Units/Units.cs index 3946be0a3..161b52a0e 100644 --- a/csharp/Lib/Units/Units.cs +++ b/csharp/Lib/Units/Units.cs @@ -1,5 +1,4 @@ using System.Collections; -using System.Reflection; using InnovEnergy.Lib.Units.Power; using InnovEnergy.Lib.Utils; using static System.Reflection.BindingFlags; @@ -89,87 +88,3 @@ public static class Units } } -public static class Prefixes -{ - private static readonly IReadOnlyList Big = new[] - { - "", - "k", - "M", - "G", - "T", - "P", - "E", - "Y", - }; - - private static readonly IReadOnlyList Small = new[] - { - "", - "m", - "µ", - "n", - "p", - "f", - "a", - "z", - "y", - }; - - public static String TestGetPrefix(Double v, String unit) - { - if (v == 0) - return ""; - - var log10 = Math.Log10(v / 10); - var l = (Int32)Math.Floor(log10 / 3); - var lookUp = l > 0 ? Big : Small; - var i = Math.Abs(l); - - return $"{v / Math.Pow(10.0, l * 3.0)} {lookUp[i]}{unit}"; - } - - - public static String TestGetPrefix(Decimal v, String unit) - { - if (v == 0m) - return ""; - - var d = (Double)v; - var log10 = Math.Log10(d / 10); - var l = (Int32)Math.Floor(log10 / 3); - var lookUp = l > 0 ? Big : Small; - var i = Math.Abs(l); - - return $"{d / Math.Pow(10.0, l * 3.0)} {lookUp[i]}{unit}"; - } - - public static String TestGetPrefix2(Decimal v, String unit) - { - if (v == 0m) - return ""; - - var a = Math.Abs(v); - var s = Math.Sign(v); - - var i = 0; - - while (a >= 10000m) - { - a /= 1000; - i++; - } - while (a < 10m) - { - a *= 1000; - i--; - } - - var lookUp = i >= 0 ? Big : Small; - - var r = Decimal.Floor(a * 10m) / 10m; - - return $"{r*s} {lookUp[Math.Abs(i)]}{unit}"; - } - -}