Why does UITextfield.text return the exact floating-point number when retrieving from an array?

I have an array in which the input to textfield.text is stored. Then I extract the text elsewhere as a float. This seems to give me the “exact number” in the text box.

Example A

   navtestAppDelegate *appDelegate = 
   (navtestAppDelegate *)[[UIApplication 
    sharedApplication] delegate];

NSMutableArray *storedData = 
 appDelegate.myData;

[storedData replaceObjectAtIndex: myIndex withObject: mytextfield.text];

float number = [[storedData objectAtIndex:myIndex] floatValue];

the if mytextfield.text input is 22.8, float numberreturns22.8

but,

Example B

[storedData replaceObjectAtIndex: myIndex withObject: @"22.8"];

float number  = [[storedData objectAtIndex:myIndex] floatValue];

float number returns 22.7999999

I do not understand why I get the exact number in example A

+4
source share
2 answers

22.8 . , , , . . , 22.8 22.7999999 , . , 22.7999999, 22.8000000, 2, .

, , , , . 22.8 .

, NSDecimal , .

pi , , - , . , pi ( ) pi.: -)

float double , .

0

: -

[storedData replaceObjectAtIndex: myIndex    
 withObject: [NSNumber 
 numberWithFloat:22.8]];
0

All Articles