You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
905
914
/// </summary>
915
+
/// <param name=""unit"">The unit to convert to.</param>
906
916
/// <returns>A {_quantity.Name} with the specified unit.</returns>
907
917
public {_quantity.Name} ToUnit({_unitEnumName} unit)
908
918
{{
909
-
var convertedValue = GetValueAs(unit);
910
-
return new {_quantity.Name}(convertedValue, unit);
919
+
return ToUnit(unit, DefaultConversionFunctions);
920
+
}}
921
+
922
+
/// <summary>
923
+
/// Converts this {_quantity.Name} to another {_quantity.Name} using the given <paramref name=""unitConverter""/> with the unit representation <paramref name=""unit"" />.
924
+
/// </summary>
925
+
/// <param name=""unit"">The unit to convert to.</param>
926
+
/// <param name=""unitConverter"">The <see cref=""UnitConverter""/> to use for the conversion.</param>
927
+
/// <returns>A {_quantity.Name} with the specified unit.</returns>
928
+
public {_quantity.Name} ToUnit({_unitEnumName} unit, UnitConverter unitConverter)
929
+
{{
930
+
if(Unit == unit)
931
+
{{
932
+
// Already in requested units.
933
+
return this;
934
+
}}
935
+
else if(unitConverter.TryGetConversionFunction((typeof({_quantity.Name}), Unit, typeof({_quantity.Name}), unit), out var conversionFunction))
936
+
{{
937
+
// Direct conversion to requested unit found. Return the converted quantity.
938
+
var converted = conversionFunction(this);
939
+
return ({_quantity.Name})converted;
940
+
}}
941
+
else if(Unit != BaseUnit)
942
+
{{
943
+
// Direct conversion to requested unit NOT found. Convert to BaseUnit, and then from BaseUnit to requested unit.
944
+
var inBaseUnits = ToUnit(BaseUnit);
945
+
return inBaseUnits.ToUnit(unit);
946
+
}}
947
+
else
948
+
{{
949
+
throw new NotImplementedException($""Can not convert {{Unit}} to {{unit}}."");
0 commit comments