Does anyone know of a good unit conversion library that works with Objective-C? Part of a small app that I'm working on requires me to take two items that are for sale and compare their prices. This means that I'll have to convert the quantities to a base unit for a valid comparison.
For example, if I have one item that can be purchased for $1 per pint and another that costs $3 per gallon, I need to be able to convert these to a common base unit.
Rather than re-invent the wheel, is there a library out there that can do the base-unit conversion for me?
Thanks!
I've just pushed my Objective-C unit conversion library to GitHub: https://github.com/HiveHicks/HHUnitConverter. Take a look at HHUnitConverter-MacOSX-Example for examples on how to use it.
The main purpose of a programming library is to abstract away general purpose, reusable code, into a higher level unit where you won't have to care about the details. But this is an area where details probably matter to your project.
Doing math for unit conversions involves a lot of trade-offs between accuracy and performance. If you use native floating point types, you need to avoid situations where a Very Large Number is added to a very small number: the small number may completely vanish. If you use a custom numerical representation, math will be an order of magnitude slower, which may or may not be a problem depending on how much math your application is doing.
Also, choosing that "common base unit" for comparisons is 1,000 times easier if you know what context you are working in. Given your example, it seems like pints or ounces or gallons or even liters might make sense. But if you were working with megaliters, a library that converted everything down to liters might lose a lot of precision. The library would either need to choose a default and be less generic, or allow changing the common unit, which would make it much more complicated (and therefore bug prone).
So, while I can't authoritatively answer your question and say no such library exists, that's why I think you haven't found one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With