Unit conversions for collections #913
-
Hi @angularsen, I've just been playing with UnitsNet and it looks like a really useful library. I'm attempting to use it to convert collections of values stored in any source unit to SI units. I've been looking through the library and didn't spot any obvious solutions for converting collections rather than individual values, I may well have missed something though. An example of the approach I've used to try this out simply to loop through the source array generating elementwise conversions, e.g.
Before I go fully down this route, I just wondered if there was a pre-baked solution for converting values in arrays/collections . Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi, there is nothing in the library for collections specifically, but your approach works and you can also consider using LINQ to simplify slightly: // LINQPad
void Main()
{
ToSI(new double[] { 1, 2, 3}, LengthUnit.Centimeter).Dump();
}
public double[] ToSI(double[] values, LengthUnit unit)
{
return values.Select(val => Length.From(val, unit).ToUnit(UnitSystem.SI).Value).ToArray();
} |
Beta Was this translation helpful? Give feedback.
Hi, there is nothing in the library for collections specifically, but your approach works and you can also consider using LINQ to simplify slightly: