Search code examples
iosobjective-ciphonedoublensdictionary

Add float value in dictionary in number format in objective c


when we add int variable in dictionary, we need to convert it in NSNumber format

int f3 = 32;
int f4 = 35;

NSNumber* num3 = [NSNumber numberWithInt:f3];
NSNumber* num4 = [NSNumber numberWithInt:f4];

NSDictionary *params1 = @{@"num3": num3, @"num4" : num4};

and when we see the dictionary value

num3 = 32;
num4 = 35;

this output comes in dictionary but when we add float value in dictionary like

float f1 = 12.123456;
float f2 = 15.123456;

NSNumber* num1 = [NSNumber numberWithFloat:f1];
NSNumber* num2 = [NSNumber numberWithFloat:f2];

NSDictionary *params = @{@"num1": num1, @"num2" : num2};

I am getting value inside dictionary are string instead of number mince i am getting value in double quote

num1 = "12.12346";
num2 = "15.12346";

Please any one help me... i want float value in dictionary but in numeric form..


Solution

  • You right!...

    float f1 = 12.123456;
    float f2 = 15.123456;
    
    NSNumber* num1 = [NSNumber numberWithFloat:f1];
    NSNumber* num2 = [NSNumber numberWithFloat:f2];
    
    NSDictionary *params = @{@"num1": num1, @"num2" : num2};
    
    
    NSLog(@"%@",[params valueForKey:@"num1"]);
    

    Please get value from dictionary then you will get your answers.

    This is look like string but value is float.

    Please check images: Check1

    check2 check3