-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ToAutoUnitString #1327
Comments
Sorry for late reply, yes this seems useful and something similar has been requested before. Please go ahead with a pull request and add a few test cases for it with different quantities and units. |
One thing, it may be better to have the method return the unit, so that we can reuse the Length.FromMillimeters(55.1234).ToAutoUnit(units2).ToString("s1"); // 5.5 cm public static TQuantity ToAutoUnit<TQuantity, TQuantityUnit>(this TQuantity quantity, TQuantityUnit[] units)
where TQuantity : IQuantity where TQuantityUnit : Enum
{
if (quantity is null) throw new ArgumentNullException(nameof(quantity));
if (units is null) throw new ArgumentNullException("Specify at least one unit, preferably multiple units.", nameof(units));
TQuantityUnit best = units.OrderBy(u => Math.Abs(Math.Log10(Math.Abs((double)quantity.ToUnit(u).Value))))
.FirstOrDefault();
return (TQuantity)quantity.ToUnit(best);
} |
Yes, agree, to have |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This issue was automatically closed due to inactivity. |
I had created my owen Extension for ToAutoUnitString(...) ... it is very simple, the extension gets order of Value in different assumed units and the best one is used for conversion:
5.5e-3 m ---> 5.5 mm
5.5 m ---> 5.5 m
5.5e3 m ---> 5.5 km
Responsibility which units have to be used is on an user (array parameter: TQuantityUnit[] assumedUnits)
Extension:
is it useful?
The text was updated successfully, but these errors were encountered: