0

My app has a picker that has numbers with suffix of lbs. I want to add support for user to be able to use kgs instead. So I tried using this:

NSNumber *weightInPounds = [pickerArray objectAtIndex:row];
NSNumber *weightInKilos = [[DDUnitConverter massUnitConverter] convertNumber: weightInPounds fromUnit: DDMassUnitUSPounds toUnit: DDMassUnitKilograms];
float roundedValue = round(2.0f * [weightInKilos floatValue]) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
label.text = numberString;
[formatter release];

Using that code, it rounds to the nearest .5 or .0. The problem is that the increments are not consistent. Like its not 1, 1.5, 2, 2.5, 3, etc. Its coming out more like 1, 2, 3.5, 4.5, 5, etc. I think this is because when lbs are converted to kgs, that is the way it comes out. But I don't think the user will want the numbers displayed like that. It seems like they would want normal increments of .5 kgs. Does anyone know how I can fix this?

  • Looks like you found [this famous question](http://stackoverflow.com/questions/752817/rounding-numbers-in-objective-c) already. – Matt Ball Jun 15 '11 at 02:55
  • Yes, but I'm not sure if that is the best way to display the data, because the increments are kind of random. –  Jun 15 '11 at 02:57

1 Answers1

3

You seem to be basing all of your calculations off of the row of the picker. If you want to do this such that picker row 1 of lbs is the same weight as picker row 1 of kg, then you will have no choice but to have it like this. A better solution is to make the wheel go from 0 − 300 without labels. Put the label somewhere else (perhaps as a second picker wheel next to it or a switch somewhere). Then you can get the value for the weight and the value for the units and do the right thing.

Better yet, you can determine the locale for the user and set the units automatically for them.

coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • Thanks, so lets say I take the unit label out of the picker and make it separate. Do you think it would be better to have component 2 have 2 rows, lb and kgs, then there would not really be a conversion going on, but rather just a different label at the end. Or should I keep a conversion, and make it so the row 1 of lg is not the same as row 1 of kgs, etc. –  Jun 15 '11 at 02:59
  • I would keep two side-by-side picker wheels like I suggested. No need for conversion here. This way lends itself to less user error and less code to write (so less chances for developer error). It seems to be a win-win from both usability and implementation standpoints. Just try to determine which unit should be selected by default by reading the users locale. Of course, I have no idea what you are trying to do but I assume it is a weight entry.. and for that, this way is a win. – coneybeare Jun 15 '11 at 03:06
  • Thanks, thats excellent. I spent the better half of today dealing with conversions but theres really no good way to do it. Thanks! –  Jun 15 '11 at 03:10