UnitsNet icon indicating copy to clipboard operation
UnitsNet copied to clipboard

Add for each Function e.g. Length a overload that accepts INumber<TSelf>

Open DevTKSS opened this issue 8 months ago • 0 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I have a Project that needs to convert from and to Database Units but use the System.Numerics lang features Currently the Length.FromCentimeters(value).As(LengthUnit.Millimeter) is breaking my code because it dont allows a INumber<TSelf> to be used for the QuantityValue which is expected as double.

Describe the solution you'd like A clear and concise description of what you want to happen.

Add a Overload to the Routines like Length.FromCentimeters to also accept a System.Numerics INumber or IFloatingPoint if the Routines inside your code need them, but currently thats not possible to use for this szenario

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

for the mentioned function I am using currently the old way with operators provided from the Numerics and use CreateChecked with e.g. 10 in this case.:

    public static TTarget ConvertValueToUnitSystem<TSource, TTarget>(TSource value, string unitName = UnitName.Millimeter)
        where TSource : struct, INumber<TSource>,IFloatingPoint<TSource>
        where TTarget : struct, INumber<TTarget>
    {
        TSource result = unitName switch
        {
            UnitName.Millimeter => value / TSource.CreateChecked(10),
            UnitName.KiloVolt => value * TSource.CreateChecked(10000000),
            UnitName.Degree => value / TSource.CreateChecked(180) * TSource.CreateChecked(Math.PI),
            UnitName.Gradian => value / TSource.CreateChecked(180) * TSource.CreateChecked(Math.PI),
            UnitName.SquareMeter => value * TSource.CreateChecked(10000),
            UnitName.CubicMeter => value * TSource.CreateChecked(1000000),
            _ => value
        };
        return TTarget.CreateChecked(result);
    }

while the "UnitName" is just a Static Class with a bunch of const string with the Unit like this:

    public const string Millimeter = "mm";

Additional context Add any other context or screenshots about the feature request here.

DevTKSS avatar Jun 01 '25 17:06 DevTKSS