@@ -113,11 +113,11 @@ public AmountOfSubstance(double moles)
113
113
#else
114
114
public
115
115
#endif
116
- AmountOfSubstance ( double numericValue , AmountOfSubstanceUnit unit )
116
+ AmountOfSubstance ( double numericValue , AmountOfSubstanceUnit unit )
117
117
{
118
118
_value = numericValue ;
119
119
_unit = unit ;
120
- }
120
+ }
121
121
122
122
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
123
123
/// <summary>
@@ -782,7 +782,7 @@ public int CompareTo(object obj)
782
782
#endif
783
783
int CompareTo ( AmountOfSubstance other )
784
784
{
785
- return AsBaseUnitMoles ( ) . CompareTo ( other . AsBaseUnitMoles ( ) ) ;
785
+ return AsBaseUnit ( ) . CompareTo ( other . AsBaseUnit ( ) ) ;
786
786
}
787
787
788
788
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -830,7 +830,7 @@ public override bool Equals(object obj)
830
830
return false ;
831
831
}
832
832
833
- return AsBaseUnitMoles ( ) . Equals ( ( ( AmountOfSubstance ) obj ) . AsBaseUnitMoles ( ) ) ;
833
+ return AsBaseUnit ( ) . Equals ( ( ( AmountOfSubstance ) obj ) . AsBaseUnit ( ) ) ;
834
834
}
835
835
836
836
/// <summary>
@@ -843,7 +843,7 @@ public override bool Equals(object obj)
843
843
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
844
844
public bool Equals ( AmountOfSubstance other , AmountOfSubstance maxError )
845
845
{
846
- return Math . Abs ( AsBaseUnitMoles ( ) - other . AsBaseUnitMoles ( ) ) <= maxError . AsBaseUnitMoles ( ) ;
846
+ return Math . Abs ( AsBaseUnit ( ) - other . AsBaseUnit ( ) ) <= maxError . AsBaseUnit ( ) ;
847
847
}
848
848
849
849
public override int GetHashCode ( )
@@ -861,14 +861,49 @@ public override int GetHashCode()
861
861
/// <returns>Value converted to the specified unit.</returns>
862
862
public double As ( AmountOfSubstanceUnit unit )
863
863
{
864
- if ( Unit == unit )
864
+ if ( Unit == unit )
865
+ return Convert . ToDouble ( Value ) ;
866
+
867
+ var converted = AsBaseNumericType ( unit ) ;
868
+ return Convert . ToDouble ( converted ) ;
869
+ }
870
+
871
+ /// <summary>
872
+ /// Converts the current value + unit to the base unit.
873
+ /// This is typically the first step in converting from one unit to another.
874
+ /// </summary>
875
+ /// <returns>The value in the base unit representation.</returns>
876
+ private double AsBaseUnit ( )
877
+ {
878
+ switch ( Unit )
865
879
{
866
- return ( double ) Value ;
880
+ case AmountOfSubstanceUnit . Centimole : return ( _value ) * 1e-2d ;
881
+ case AmountOfSubstanceUnit . CentipoundMole : return ( _value * 453.59237 ) * 1e-2d ;
882
+ case AmountOfSubstanceUnit . Decimole : return ( _value ) * 1e-1d ;
883
+ case AmountOfSubstanceUnit . DecipoundMole : return ( _value * 453.59237 ) * 1e-1d ;
884
+ case AmountOfSubstanceUnit . Kilomole : return ( _value ) * 1e3d ;
885
+ case AmountOfSubstanceUnit . KilopoundMole : return ( _value * 453.59237 ) * 1e3d ;
886
+ case AmountOfSubstanceUnit . Micromole : return ( _value ) * 1e-6d ;
887
+ case AmountOfSubstanceUnit . MicropoundMole : return ( _value * 453.59237 ) * 1e-6d ;
888
+ case AmountOfSubstanceUnit . Millimole : return ( _value ) * 1e-3d ;
889
+ case AmountOfSubstanceUnit . MillipoundMole : return ( _value * 453.59237 ) * 1e-3d ;
890
+ case AmountOfSubstanceUnit . Mole : return _value ;
891
+ case AmountOfSubstanceUnit . Nanomole : return ( _value ) * 1e-9d ;
892
+ case AmountOfSubstanceUnit . NanopoundMole : return ( _value * 453.59237 ) * 1e-9d ;
893
+ case AmountOfSubstanceUnit . PoundMole : return _value * 453.59237 ;
894
+ default :
895
+ throw new NotImplementedException ( $ "Can not convert { Unit } to base units.") ;
867
896
}
897
+ }
898
+
899
+ private double AsBaseNumericType ( AmountOfSubstanceUnit unit )
900
+ {
901
+ if ( Unit == unit )
902
+ return _value ;
868
903
869
- double baseUnitValue = AsBaseUnitMoles ( ) ;
904
+ var baseUnitValue = AsBaseUnit ( ) ;
870
905
871
- switch ( unit )
906
+ switch ( unit )
872
907
{
873
908
case AmountOfSubstanceUnit . Centimole : return ( baseUnitValue ) / 1e-2d ;
874
909
case AmountOfSubstanceUnit . CentipoundMole : return ( baseUnitValue / 453.59237 ) / 1e-2d ;
@@ -884,9 +919,8 @@ public double As(AmountOfSubstanceUnit unit)
884
919
case AmountOfSubstanceUnit . Nanomole : return ( baseUnitValue ) / 1e-9d ;
885
920
case AmountOfSubstanceUnit . NanopoundMole : return ( baseUnitValue / 453.59237 ) / 1e-9d ;
886
921
case AmountOfSubstanceUnit . PoundMole : return baseUnitValue / 453.59237 ;
887
-
888
922
default :
889
- throw new NotImplementedException ( "unit: " + unit ) ;
923
+ throw new NotImplementedException ( $ "Can not convert { Unit } to { unit } ." ) ;
890
924
}
891
925
}
892
926
@@ -1236,39 +1270,6 @@ public string ToString(
1236
1270
/// </summary>
1237
1271
public static AmountOfSubstance MinValue => new AmountOfSubstance ( double . MinValue , BaseUnit ) ;
1238
1272
1239
- /// <summary>
1240
- /// Converts the current value + unit to the base unit.
1241
- /// This is typically the first step in converting from one unit to another.
1242
- /// </summary>
1243
- /// <returns>The value in the base unit representation.</returns>
1244
- private double AsBaseUnitMoles ( )
1245
- {
1246
- if ( Unit == AmountOfSubstanceUnit . Mole ) { return _value ; }
1247
-
1248
- switch ( Unit )
1249
- {
1250
- case AmountOfSubstanceUnit . Centimole : return ( _value ) * 1e-2d ;
1251
- case AmountOfSubstanceUnit . CentipoundMole : return ( _value * 453.59237 ) * 1e-2d ;
1252
- case AmountOfSubstanceUnit . Decimole : return ( _value ) * 1e-1d ;
1253
- case AmountOfSubstanceUnit . DecipoundMole : return ( _value * 453.59237 ) * 1e-1d ;
1254
- case AmountOfSubstanceUnit . Kilomole : return ( _value ) * 1e3d ;
1255
- case AmountOfSubstanceUnit . KilopoundMole : return ( _value * 453.59237 ) * 1e3d ;
1256
- case AmountOfSubstanceUnit . Micromole : return ( _value ) * 1e-6d ;
1257
- case AmountOfSubstanceUnit . MicropoundMole : return ( _value * 453.59237 ) * 1e-6d ;
1258
- case AmountOfSubstanceUnit . Millimole : return ( _value ) * 1e-3d ;
1259
- case AmountOfSubstanceUnit . MillipoundMole : return ( _value * 453.59237 ) * 1e-3d ;
1260
- case AmountOfSubstanceUnit . Mole : return _value ;
1261
- case AmountOfSubstanceUnit . Nanomole : return ( _value ) * 1e-9d ;
1262
- case AmountOfSubstanceUnit . NanopoundMole : return ( _value * 453.59237 ) * 1e-9d ;
1263
- case AmountOfSubstanceUnit . PoundMole : return _value * 453.59237 ;
1264
- default :
1265
- throw new NotImplementedException ( "Unit not implemented: " + Unit ) ;
1266
- }
1267
- }
1268
-
1269
- /// <summary>Convenience method for working with internal numeric type.</summary>
1270
- private double AsBaseNumericType ( AmountOfSubstanceUnit unit ) => Convert . ToDouble ( As ( unit ) ) ;
1271
-
1272
1273
/// <summary>
1273
1274
/// The <see cref="BaseDimensions" /> of this quantity.
1274
1275
/// </summary>
0 commit comments