ToStringRounded => ToDisplayString, introduce scientific notation (mW, kW,...) for units

This commit is contained in:
ig 2023-06-27 17:02:56 +02:00
parent a761e46686
commit 5484aea6cb
4 changed files with 39 additions and 92 deletions

View File

@ -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);

View File

@ -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" };
}

View File

@ -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}";
}
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<String> Prefix = new[] { "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Y" };
}

View File

@ -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<String> Big = new[]
{
"",
"k",
"M",
"G",
"T",
"P",
"E",
"Y",
};
private static readonly IReadOnlyList<String> 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}";
}
}