1

I have a UIPickerView that I am populating with an array. It has two columns and i need the first column to go from 50-500. the second column to go from 0.01 to 1. The point is for the user to pick their weight. For doing the 50-500 I have this,

-(void)populateWeightPickerArray{
weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight <=500; weight++) {
    NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
    [weightPickerArray addObject:weightString];
}
}  

I tried doing that with the decimal, however when i use ++ it goes up by whole number and I end up getting 1.01, 2.01, 3.01 etc. here is what I have for code.

-(void)populateWeightPickerArray2{
weightPickerArray2 = [[NSMutableArray alloc] init];
for (float weightDecimal = .01; weightDecimal <= 10; weightDecimal++) {
    NSString *weightDecimalString = [NSString stringWithFormat:@"%0.2f",weightDecimal];
    [weightPickerArray2 addObject:weightDecimalString];
}   
}

(I know i said I only needed it to go to 1 not 10, but i put 10 because at first it was only displaying 1.01, so i put 10 to test the output until I can get it right.)

So i need to somehow increment it to make it go from .01 to 1 (0.02, 0.03,0.04 etc). Anyone know how to do this?

user1450221
  • 109
  • 2
  • 12

1 Answers1

0
for (float weightDecimal = .01; weightDecimal <= 10; weightDecimal = weightDecimal + 0.01)

or more succinctly:

for (float weightDecimal = .01; weightDecimal <= 10; weightDecimal += 0.01)

In Objective C: x += y is a shorthand for x = x + y.

Although you often see for loops with something++ or something-- you can use any expression.

idz
  • 12,825
  • 1
  • 29
  • 40
  • What do I use for the string format.. "NSString string with format:???".... I have tried @"%0.01f" and that results in only one decimal being shown (0.1). I have tried several other things but cannot get the correct format(0.01, 0.02, 0.03,...0.11, 0.12, etc.) @idz – user1450221 Aug 01 '12 at 05:10
  • Your original `@"%0.2f"` should have been OK. (The 0 before the decimal point means you want to show a zero before the decimal point when appropriate and the 2 after means to show two decimal places.) – idz Aug 01 '12 at 06:07