Quantity from value and unit-abbreviation #1252
-
Up to now, I was not able to find a way to create a Quantity based on a value and its unit abbreviation. Am I missing something? I'm aware that there might be ambigues abbreviations, but for the majority of the cases this should be working fine though. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, sorry for the late reply. If you are looking to serialize data to storage and read it back, I would recommend this instead: IQuantity quantity = Quantity.From(value: 3, quantityName: "Length", unitName: "Centimeter");
if (Quantity.TryFrom(value: 3, quantityName: "Length", unitName: "Centimeter", out IQuantity? quantity2))
{
} If you still need to store and read unit abbreviations, which I recommend against, then I just merged a naive feature for this you could try. There is a relatively high chance of failure due to multiple units (across all quantities) having the same abbreviation. New example in the README: // Length with unit LengthUnit.Centimeter
IQuantity quantity = Quantity.FromUnitAbbreviation(3, "cm");
if (Quantity.TryFromUnitAbbreviation(3, "cm", out IQuantity? quantity2))
{
} Let me know how this works for you. Nuget should be out shortly. |
Beta Was this translation helpful? Give feedback.
Hi, sorry for the late reply.
If you are looking to serialize data to storage and read it back, I would recommend this instead:
If you still need to store and read unit abbreviations, which I recommend against, then I just merged a naive feature for this you could try.
#1265
There is a relatively high chance of failure due to multiple units (across all quantities) having the same abbreviation.
New example in the README: