0

In my app, numbers are displayed as pounds. I need to add an option to use kilograms instead. Does anyone know how to go about this? I'm using Core Data to store numbers. I know I can do it all manually by using an if statement then doing a conversion, but there must be better way.

EDIT Actually, I think I ran into a huge problem. It isn't as simple as converting numbers. Because I am using a picker and I want to offer international imperial support, The picker should display whole, sensible numbers in kg. If I just convert the numbers to kg, I will get decimal numbers which people will not know how to use. Any suggestions?

4 Answers4

5

https://github.com/davedelong/DDUnitConverter

NSNumber *weightInPounds = [NSNumber numberWithInt:42];
NSNumber *weightInKilos = [[DDUnitConverter massUnitConverter] convertNumber:weightInPounds fromUnit:DDMassUnitUSPounds toUnit:DDMassUnitKilograms];

Pretty simple.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1

I would suggest using a custom NSFormatter to convert your numbers into strings for display. You would always store your data in one format, while providing the ability to display it in any format. You will need a simple setting (stored via NSUserDefaults) to tell the formatter what format to use. Some benefits of this are that you only have to deal with this while displaying the numbers. Since you already have to convert the numbers to strings to display them, there will be very few changes to your code. Also, you won't have to change your core data stores at all, since the setting is stored in the app's preferences. You can even subclass NSNumberFormatter for automatic formatting of the number after conversion. Here is an example implementation:

- (NSString *)stringForObjectValue:(NSNumber *)number {
    double val = [number doubleValue];
    BOOL isKGs = [[NSUserDefaults standardUserDefaults] boolForKey:@"wantsKGs"];
    if(isKGs) val *= 0.45359237;
    NSString *str = [super stringForObjectValue:[NSNumber numberWithDouble:val]];
    if(isKGs) return [str stringByAppendingString:@" kg"];
    return [str stringByAppendingString:@" lbs"];
}

For more information about custom formatters, see Creating a Custom Formatter.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • 1
    +1 A good place to park the custom formatter is inside a class method of the NSManagedObject subclass that uses it. Then you always have the formatter easily at hand anywhere you might use it. – TechZen Jun 10 '11 at 14:02
0

there are so many ways you could do this.

I'd have to make some assumptions about your app, but if you want to have the option to change (globally) from lbs to kgs then i would probably have a settings dialog in your app so that a user can persist their decision to see things in lbs or kgs, i would then persist this in settings for your app and use this setting as part of your app's initialization. then, wherever you show a value of lbs or kgs you just need to check for the user defined setting and adjust the values accordingly (and probably any labels in the app as well) this would be as simple as having a check in the code that retrieves the values from core date

Check setting persist this as a globally accessible piece of state; this could be a member in your app delegate, a singleton that contains settings or simply requesting the setting wherever you need to check it. Then, in code that access the lbs data, check the setting, if its set to kg do the calculation and return that back from the data access code to the view.

Matt
  • 4,253
  • 1
  • 27
  • 29
0

It may be a good idea to code it to work always with the units and scaling as variables.

The units and scaling would be "lbs" and 1.0, or "kgs" and 0.45359237, or whatever.

MRAB
  • 20,356
  • 6
  • 40
  • 33