forked from angularsen/UnitsNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitConverter.cs
554 lines (495 loc) · 30.8 KB
/
UnitConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Linq;
using UnitsNet.InternalHelpers;
using UnitsNet.Units;
namespace UnitsNet
{
using ConversionFunctionLookupKey = ValueTuple<Type, Enum, Type, Enum>;
/// <summary>
///
/// </summary>
/// <param name="inputValue"></param>
/// <returns></returns>
public delegate IQuantity ConversionFunction(IQuantity inputValue);
/// <summary>
///
/// </summary>
/// <typeparam name="TQuantity"></typeparam>
/// <param name="inputValue"></param>
/// <returns></returns>
public delegate TQuantity ConversionFunction<TQuantity>(TQuantity inputValue)
where TQuantity : IQuantity;
/// <summary>
/// Convert between units of a quantity, such as converting from meters to centimeters of a given length.
/// </summary>
public sealed class UnitConverter
{
/// <summary>
/// The static instance used by Units.NET to convert between units. Modify this to add/remove conversion functions at runtime, such
/// as adding your own third-party units and quantities to convert between.
/// </summary>
[Obsolete("Use UnitsNetSetup.Default.UnitConverter instead.")]
public static UnitConverter Default => UnitsNetSetup.Default.UnitConverter;
/// <summary>
/// Creates a new <see cref="UnitConverter"/> instance.
/// </summary>
public UnitConverter()
{
ConversionFunctions = new ConcurrentDictionary<ConversionFunctionLookupKey, ConversionFunction>();
}
/// <summary>
/// Creates a new <see cref="UnitConverter"/> instance with the <see cref="ConversionFunction"/> copied from <paramref name="other"/>.
/// </summary>
/// <param name="other">The <see cref="UnitConverter"/> to copy from.</param>
public UnitConverter(UnitConverter other)
{
ConversionFunctions = new ConcurrentDictionary<ConversionFunctionLookupKey, ConversionFunction>(other.ConversionFunctions);
}
/// <summary>
/// Create an instance of the unit converter with all the built-in unit conversions defined in the library.
/// </summary>
/// <returns>The unit converter.</returns>
public static UnitConverter CreateDefault()
{
var unitConverter = new UnitConverter();
RegisterDefaultConversions(unitConverter);
return unitConverter;
}
private ConcurrentDictionary<ConversionFunctionLookupKey, ConversionFunction> ConversionFunctions
{
get;
}
/// <summary>
/// Registers the default conversion functions in the given <see cref="UnitConverter"/> instance.
/// </summary>
/// <param name="unitConverter">The <see cref="UnitConverter"/> to register the default conversion functions in.</param>
public static void RegisterDefaultConversions(UnitConverter unitConverter)
{
if (unitConverter is null)
throw new ArgumentNullException(nameof(unitConverter));
foreach(var quantity in Quantity.GetQuantityTypes())
{
var registerMethod = quantity.GetMethod(nameof(Length.RegisterDefaultConversions), BindingFlags.NonPublic | BindingFlags.Static);
registerMethod?.Invoke(null, new object[]{unitConverter});
}
}
/// <summary>
/// Sets the conversion function from two units of the same quantity type.
/// </summary>
/// <typeparam name="TQuantity">The type of quantity, must implement <see cref="IQuantity"/>.</typeparam>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
public void SetConversionFunction<TQuantity>(Enum from, Enum to, ConversionFunction<TQuantity> conversionFunction)
where TQuantity : IQuantity
{
var quantityType = typeof(TQuantity);
var conversionLookup = new ConversionFunctionLookupKey(quantityType, from, quantityType, to);
SetConversionFunction(conversionLookup, conversionFunction);
}
/// <summary>
/// Sets the conversion function from two units of different quantity types.
/// </summary>
/// <typeparam name="TQuantityFrom">From quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <typeparam name="TQuantityTo">To quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
public void SetConversionFunction<TQuantityFrom, TQuantityTo>(Enum from, Enum to, ConversionFunction conversionFunction)
where TQuantityFrom : IQuantity
where TQuantityTo : IQuantity
{
SetConversionFunction(typeof(TQuantityFrom), from, typeof(TQuantityTo), to, conversionFunction);
}
/// <summary>
/// Sets the conversion function from two units of different quantity types.
/// </summary>
/// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
public void SetConversionFunction(Type fromType, Enum from, Type toType, Enum to, ConversionFunction conversionFunction)
{
var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to);
SetConversionFunction(conversionLookup, conversionFunction);
}
/// <summary>
/// Sets the conversion function for a particular conversion function lookup.
/// </summary>
/// <param name="lookupKey">The lookup key.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
internal void SetConversionFunction(ConversionFunctionLookupKey lookupKey, ConversionFunction conversionFunction)
{
ConversionFunctions[lookupKey] = conversionFunction;
}
/// <summary>
/// Sets the conversion function for a particular conversion function lookup.
/// </summary>
/// <typeparam name="TQuantity">The quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <param name="conversionLookup">The quantity conversion function lookup key.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
internal void SetConversionFunction<TQuantity>(ConversionFunctionLookupKey conversionLookup, ConversionFunction<TQuantity> conversionFunction)
where TQuantity : IQuantity
{
IQuantity TypelessConversionFunction(IQuantity quantity) => conversionFunction((TQuantity) quantity);
ConversionFunctions[conversionLookup] = TypelessConversionFunction;
}
/// <summary>
/// Gets the conversion function from two units of the same quantity type.
/// </summary>
/// <typeparam name="TQuantity">The quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <returns></returns>
public ConversionFunction GetConversionFunction<TQuantity>(Enum from, Enum to) where TQuantity : IQuantity
{
return GetConversionFunction(typeof(TQuantity), from, typeof(TQuantity), to);
}
/// <summary>
/// Gets the conversion function from two units of different quantity types.
/// </summary>
/// <typeparam name="TQuantityFrom">From quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <typeparam name="TQuantityTo">To quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <returns></returns>
public ConversionFunction GetConversionFunction<TQuantityFrom, TQuantityTo>(Enum from, Enum to)
where TQuantityFrom : IQuantity
where TQuantityTo : IQuantity
{
return GetConversionFunction(typeof(TQuantityFrom), from, typeof(TQuantityTo), to);
}
/// <summary>
/// Gets the conversion function from two units of different quantity types.
/// </summary>
/// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
public ConversionFunction GetConversionFunction(Type fromType, Enum from, Type toType, Enum to)
{
var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to);
return GetConversionFunction(conversionLookup);
}
/// <summary>
/// Gets the conversion function by its lookup key.
/// </summary>
/// <param name="lookupKey"></param>
internal ConversionFunction GetConversionFunction(ConversionFunctionLookupKey lookupKey)
{
IQuantity EchoFunction(IQuantity fromQuantity) => fromQuantity;
// If from/to units and to quantity types are equal, then return a function that echoes the input quantity
// in order to not have to map conversion functions to "self".
if (lookupKey.Item1 == lookupKey.Item3 && Equals(lookupKey.Item2, lookupKey.Item4))
return EchoFunction;
return ConversionFunctions[lookupKey];
}
/// <summary>
/// Gets the conversion function for two units of the same quantity type.
/// </summary>
/// <typeparam name="TQuantity">The quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
/// <returns>true if set; otherwise, false.</returns>
public bool TryGetConversionFunction<TQuantity>(Enum from, Enum to, [NotNullWhen(true)] out ConversionFunction? conversionFunction) where TQuantity : IQuantity
{
return TryGetConversionFunction(typeof(TQuantity), from, typeof(TQuantity), to, out conversionFunction);
}
/// <summary>
/// Gets the conversion function for two units of different quantity types.
/// </summary>
/// <typeparam name="TQuantityFrom">From quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <typeparam name="TQuantityTo">To quantity type, must implement <see cref="IQuantity"/>.</typeparam>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
/// <returns>true if set; otherwise, false.</returns>
public bool TryGetConversionFunction<TQuantityFrom, TQuantityTo>(Enum from, Enum to, [NotNullWhen(true)] out ConversionFunction? conversionFunction)
where TQuantityFrom : IQuantity
where TQuantityTo : IQuantity
{
return TryGetConversionFunction(typeof(TQuantityFrom), from, typeof(TQuantityTo), to, out conversionFunction);
}
/// <summary>
/// Try to get the conversion function for two units of the same quantity type.
/// </summary>
/// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param>
/// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
/// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param>
/// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
/// <param name="conversionFunction">The quantity conversion function.</param>
/// <returns>true if set; otherwise, false.</returns>
public bool TryGetConversionFunction(Type fromType, Enum from, Type toType, Enum to, [NotNullWhen(true)] out ConversionFunction? conversionFunction)
{
var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to);
return TryGetConversionFunction(conversionLookup, out conversionFunction);
}
/// <summary>
///
/// </summary>
/// <param name="lookupKey"></param>
/// <param name="conversionFunction"></param>
/// <returns>true if set; otherwise, false.</returns>
public bool TryGetConversionFunction(ConversionFunctionLookupKey lookupKey, [NotNullWhen(true)] out ConversionFunction? conversionFunction)
{
return ConversionFunctions.TryGetValue(lookupKey, out conversionFunction);
}
/// <summary>
/// Convert between any two quantity units given a numeric value and two unit enum values.
/// </summary>
/// <param name="fromValue">Numeric value.</param>
/// <param name="fromUnitValue">From unit enum value.</param>
/// <param name="toUnitValue">To unit enum value, must be compatible with <paramref name="fromUnitValue" />.</param>
/// <returns>The converted value in the new unit representation.</returns>
public static double Convert(QuantityValue fromValue, Enum fromUnitValue, Enum toUnitValue)
{
return Quantity
.From(fromValue, fromUnitValue)
.As(toUnitValue);
}
/// <summary>
/// Try to convert between any two quantity units given a numeric value and two unit enum values.
/// </summary>
/// <param name="fromValue">Numeric value.</param>
/// <param name="fromUnitValue">From unit enum value.</param>
/// <param name="toUnitValue">To unit enum value, must be compatible with <paramref name="fromUnitValue" />.</param>
/// <param name="convertedValue">The converted value, if successful. Otherwise default.</param>
/// <returns>True if successful.</returns>
public static bool TryConvert(QuantityValue fromValue, Enum fromUnitValue, Enum toUnitValue, out double convertedValue)
{
convertedValue = 0;
if (!Quantity.TryFrom(fromValue, fromUnitValue, out IQuantity? fromQuantity))
return false;
try
{
// We're not going to implement TryAs() in all quantities, so let's just try-catch here
convertedValue = fromQuantity.As(toUnitValue);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter".
/// This is particularly useful for creating things like a generated unit conversion UI,
/// where you list some selectors:
/// a) Quantity: Length, Mass, Force etc.
/// b) From unit: Meter, Centimeter etc if Length is selected
/// c) To unit: Meter, Centimeter etc if Length is selected
/// </summary>
/// <param name="fromValue">
/// Input value, which together with <paramref name="fromUnit" /> represents the quantity to
/// convert from.
/// </param>
/// <param name="quantityName">The invariant quantity name, such as "Length". Does not support localization.</param>
/// <param name="fromUnit">The invariant unit enum name, such as "Meter". Does not support localization.</param>
/// <param name="toUnit">The invariant unit enum name, such as "Meter". Does not support localization.</param>
/// <example>double centimeters = ConvertByName(5, "Length", "Meter", "Centimeter"); // 500</example>
/// <returns>Output value as the result of converting to <paramref name="toUnit" />.</returns>
/// <exception cref="UnitNotFoundException">No units match the abbreviation.</exception>
/// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation.</exception>
public static double ConvertByName(QuantityValue fromValue, string quantityName, string fromUnit, string toUnit)
{
if (!TryParseUnit(quantityName, toUnit, out Enum? toUnitValue)) // ex: LengthUnit.Centimeter
{
throw new UnitNotFoundException($"Unit not found [{toUnit}] for quantity [{quantityName}].") { Data = { ["unitName"] = toUnit } };
}
return Quantity.From(fromValue, quantityName, fromUnit).As(toUnitValue);
}
/// <summary>
/// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter".
/// This is particularly useful for creating things like a generated unit conversion UI,
/// where you list some selectors:
/// a) Quantity: Length, Mass, Force etc.
/// b) From unit: Meter, Centimeter etc if Length is selected
/// c) To unit: Meter, Centimeter etc if Length is selected
/// </summary>
/// <param name="inputValue">
/// Input value, which together with <paramref name="fromUnit" /> represents the quantity to
/// convert from.
/// </param>
/// <param name="quantityName">The invariant quantity name, such as "Length". Does not support localization.</param>
/// <param name="fromUnit">The invariant unit enum name, such as "Meter". Does not support localization.</param>
/// <param name="toUnit">The invariant unit enum name, such as "Meter". Does not support localization.</param>
/// <param name="result">Result if conversion was successful, 0 if not.</param>
/// <example>bool ok = TryConvertByName(5, "Length", "Meter", "Centimeter", out double centimeters); // 500</example>
/// <returns>True if conversion was successful.</returns>
public static bool TryConvertByName(QuantityValue inputValue, string quantityName, string fromUnit, string toUnit, out double result)
{
result = 0d;
if (!TryGetUnitType(quantityName, out Type? unitType))
return false;
if (!TryParseUnit(unitType, fromUnit, out Enum? fromUnitValue)) // ex: LengthUnit.Meter
return false;
if (!TryParseUnit(unitType, toUnit, out Enum? toUnitValue)) // ex: LengthUnit.Centimeter
return false;
if (!Quantity.TryFrom(inputValue, fromUnitValue, out IQuantity? quantity))
return false;
result = quantity.As(toUnitValue);
return true;
}
/// <summary>
/// Convert between any two quantity units by their abbreviations, such as converting a "Length" of N "m" to "cm".
/// This is particularly useful for creating things like a generated unit conversion UI,
/// where you list some selectors:
/// a) Quantity: Length, Mass, Force etc.
/// b) From unit: Meter, Centimeter etc if Length is selected
/// c) To unit: Meter, Centimeter etc if Length is selected
/// </summary>
/// <param name="fromValue">
/// Input value, which together with <paramref name="fromUnitAbbrev" /> represents the quantity to
/// convert from.
/// </param>
/// <param name="quantityName">The invariant quantity name, such as "Length". Does not support localization.</param>
/// <param name="fromUnitAbbrev">The abbreviation of the unit in the thread's current culture, such as "m".</param>
/// <param name="toUnitAbbrev">The abbreviation of the unit in the thread's current culture, such as "m".</param>
/// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example>
/// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns>
public static double ConvertByAbbreviation(QuantityValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev)
{
return ConvertByAbbreviation(fromValue, quantityName, fromUnitAbbrev, toUnitAbbrev, null);
}
/// <summary>
/// Convert between any two quantity units by their abbreviations, such as converting a "Length" of N "m" to "cm".
/// This is particularly useful for creating things like a generated unit conversion UI,
/// where you list some selectors:
/// a) Quantity: Length, Mass, Force etc.
/// b) From unit: Meter, Centimeter etc if Length is selected
/// c) To unit: Meter, Centimeter etc if Length is selected
/// </summary>
/// <param name="fromValue">
/// Input value, which together with <paramref name="fromUnitAbbrev" /> represents the quantity to
/// convert from.
/// </param>
/// <param name="quantityName">The invariant quantity name, such as "Length". Does not support localization.</param>
/// <param name="fromUnitAbbrev">The abbreviation of the unit in the given culture, such as "m".</param>
/// <param name="toUnitAbbrev">The abbreviation of the unit in the given culture, such as "m".</param>
/// <param name="culture">Culture to parse abbreviations with.</param>
/// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example>
/// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns>
/// <exception cref="UnitNotFoundException">
/// No unit types match the prefix of <paramref name="quantityName" /> or no units
/// are mapped to the abbreviation.
/// </exception>
/// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation.</exception>
public static double ConvertByAbbreviation(QuantityValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, string? culture)
{
if (!TryGetUnitType(quantityName, out Type? unitType))
throw new UnitNotFoundException($"The unit type for the given quantity was not found: {quantityName}");
var cultureInfo = CultureHelper.GetCultureOrInvariant(culture);
var fromUnit = UnitParser.Default.Parse(fromUnitAbbrev, unitType, cultureInfo); // ex: ("m", LengthUnit) => LengthUnit.Meter
var fromQuantity = Quantity.From(fromValue, fromUnit);
var toUnit = UnitParser.Default.Parse(toUnitAbbrev, unitType, cultureInfo); // ex:("cm", LengthUnit) => LengthUnit.Centimeter
return fromQuantity.As(toUnit);
}
/// <summary>
/// Convert between any two quantity units by their abbreviations, such as converting a "Length" of N "m" to "cm".
/// This is particularly useful for creating things like a generated unit conversion UI,
/// where you list some selectors:
/// a) Quantity: Length, Mass, Force etc.
/// b) From unit: Meter, Centimeter etc if Length is selected
/// c) To unit: Meter, Centimeter etc if Length is selected
/// </summary>
/// <param name="fromValue">
/// Input value, which together with <paramref name="fromUnitAbbrev" /> represents the quantity to
/// convert from.
/// </param>
/// <param name="quantityName">The invariant quantity name, such as "Length". Does not support localization.</param>
/// <param name="fromUnitAbbrev">The abbreviation of the unit in the thread's current culture, such as "m".</param>
/// <param name="toUnitAbbrev">The abbreviation of the unit in the thread's current culture, such as "m".</param>
/// <param name="result">Result if conversion was successful, 0 if not.</param>
/// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example>
/// <returns>True if conversion was successful.</returns>
public static bool TryConvertByAbbreviation(QuantityValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, out double result)
{
return TryConvertByAbbreviation(fromValue, quantityName, fromUnitAbbrev, toUnitAbbrev, out result, null);
}
/// <summary>
/// Convert between any two quantity units by their abbreviations, such as converting a "Length" of N "m" to "cm".
/// This is particularly useful for creating things like a generated unit conversion UI,
/// where you list some selectors:
/// a) Quantity: Length, Mass, Force etc.
/// b) From unit: Meter, Centimeter etc if Length is selected
/// c) To unit: Meter, Centimeter etc if Length is selected
/// </summary>
/// <param name="fromValue">
/// Input value, which together with <paramref name="fromUnitAbbrev" /> represents the quantity to
/// convert from.
/// </param>
/// <param name="quantityName">The invariant quantity name, such as "Length". Does not support localization.</param>
/// <param name="fromUnitAbbrev">The abbreviation of the unit in the given culture, such as "m".</param>
/// <param name="toUnitAbbrev">The abbreviation of the unit in the given culture, such as "m".</param>
/// <param name="culture">Culture to parse abbreviations with.</param>
/// <param name="result">Result if conversion was successful, 0 if not.</param>
/// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example>
/// <returns>True if conversion was successful.</returns>
public static bool TryConvertByAbbreviation(QuantityValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, out double result,
string? culture)
{
result = 0d;
if (!TryGetUnitType(quantityName, out Type? unitType))
return false;
var cultureInfo = CultureHelper.GetCultureOrInvariant(culture);
if (!UnitParser.Default.TryParse(fromUnitAbbrev, unitType, cultureInfo, out Enum? fromUnit)) // ex: ("m", LengthUnit) => LengthUnit.Meter
return false;
if (!UnitParser.Default.TryParse(toUnitAbbrev, unitType, cultureInfo, out Enum? toUnit)) // ex:("cm", LengthUnit) => LengthUnit.Centimeter
return false;
var fromQuantity = Quantity.From(fromValue, fromUnit);
result = fromQuantity.As(toUnit);
return true;
}
/// <summary>
/// Try to parse a unit by the unit enum type <paramref name="unitType" /> and a unit enum value <paramref name="unitName" />>
/// </summary>
/// <param name="unitType">Unit type, such as <see cref="LengthUnit" />.</param>
/// <param name="unitName">Unit name, such as "Meter" corresponding to <see cref="LengthUnit.Meter" />.</param>
/// <param name="unitValue">The return enum value, such as <see cref="LengthUnit.Meter" /> boxed as an object.</param>
/// <returns>True if succeeded, otherwise false.</returns>
/// <exception cref="UnitNotFoundException">No unit values match the <paramref name="unitName" />.</exception>
// TODO Move to Quantity.
internal static bool TryParseUnit(Type unitType, string unitName, [NotNullWhen(true)] out Enum? unitValue)
{
unitValue = null;
var eNames = Enum.GetNames(unitType);
var matchedUnitName = eNames.FirstOrDefault(x => x.Equals(unitName, StringComparison.OrdinalIgnoreCase));
if (matchedUnitName == null)
return false;
unitValue = (Enum) Enum.Parse(unitType, matchedUnitName);
return true;
}
/// <summary>
/// Try to parse a unit enum value by its quantity and unit names.
/// </summary>
/// <param name="quantityName">The invariant quantity name, such as "Length". Does not support localization.</param>
/// <param name="unitName">The invariant unit enum name, such as "Meter". Does not support localization.</param>
/// <param name="unitValue">The return enum value, such as <see cref="LengthUnit.Meter" /> boxed as an object.</param>
/// <returns>True if succeeded, otherwise false.</returns>
/// <exception cref="UnitNotFoundException">No unit values match the <paramref name="unitName" />.</exception>
// TODO Move to Quantity.
internal static bool TryParseUnit(string quantityName, string unitName, [NotNullWhen(true)] out Enum? unitValue)
{
unitValue = default;
// Get enum type for unit of this quantity, f.ex. LengthUnit for quantity Length.
// Then try to parse the unit enum value.
return TryGetUnitType(quantityName, out Type? unitType) &&
TryParseUnit(unitType, unitName, out unitValue);
}
// TODO Move to Quantity.
internal static bool TryGetUnitType(string quantityName, [NotNullWhen(true)] out Type? unitType)
{
var quantityInfo = Quantity.Infos.FirstOrDefault(info => info.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase));
unitType = quantityInfo?.UnitType;
return quantityInfo != null;
}
}
}