HHUnitConverter 2.0

HHUnitConverter 2.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.



  • By
  • HiveHicks

Unit conversion library for Objective-C

Usage

Conversions in the library are based on formula y = A * x + B, where x is a source value, y is a target value, A is a multiplier and B is a summand in a conversion rule.

For instance, miles can be converted to kilometers by multiplying x by 1.6. So, in this case A = 1.6 and B = 0. Kelvin value is converted to Celcius by subtracting 273, so for Kelvin-to-Celsius conversion: A = 1, B = -273. To specify the rule you have a generic setConversionRule:fromUnit:toUnit: method, but you are more likely to use convenience methods that start with letUnit:convertToUnit:...

To use conversion library you first need to create an instance of HHUnitConverter and set convertion rules that you need:

HHUnitConverter *converter = [HHUnitConverter new];
[converter letUnit:@"mi" convertToUnit:@"km" byMultiplyingBy:1.609344];
[converter letUnit:@"km" convertToUnit:@"m" byMultiplyingBy:1000];
[converter letUnit:@"m" convertToUnit:@"cm" byMultiplyingBy:100];

Then you can use converter object to convert values however you like. Converter is clever enough to find out dependencies between units that you've registered conversion rules with (as well as backward dependencies), so when you say that miles can be converted to kilometers, and kilometers can be converted to meters, it knows how to convert miles to meters correctly (and vice versa), so it's OK to have a call like that:

[converter value:482803.2 convertedFromUnit:@"m" toUnit:@"mi"]

Of course, it will handle the simplest cases like that:

[converter value:300 convertedFromUnit:@"mi" toUnit:@"km"]

Also, the library can handle compound units conversion, so, for instance, you can convert "litres per kilometer" to "gallons per mile" using code like that:

[converter letUnit:@"mi" convertToUnit:@"km" byMultiplyingBy:1.609344];
[converter letUnit:@"gal" convertToUnit:@"L" byMultiplyingBy:3.78541178];
[converter value:20 convertedFromUnit:@"L/km" toUnit:@"gal/mi"];