Tuesday, May 14, 2013

NSString floatValue example ios


[NSString floatValue]

Returns the floating-point value of the receiver’s text as a float.
- (float)floatValue
Return Value of [NSString floatValue]
The floating-point value of the receiver’s text as a float, skipping whitespace at the beginning of the string. ReturnsHUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Also returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.
Discussion of [NSString floatValue]
This method uses formatting information stored in the non-localized value; use an NSScanner object for localized scanning of numeric values from a string.
Example of [NSString floatValue]
NSString *Stringa = @"9.9999999";
NSDecimalNumber *Decimal = [[NSDecimalNumber alloc] initWithString:Stringa];
NSLog(@"Var Float: %f",[Decimal floatValue]);
NSLog(@"Var Float: %@",[Decimal stringValue]);
Example of [NSString floatValue]
//test
NSString *string = @"123,456,789";
NSString *cleanString = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
float originalValue2 = [cleanString  floatValue];
NSLog(@"datacellR2C2 as text --> %@ <---\n",cleanString);  // this correctly shows the value in datacellR2C2
NSLog(@"originalValue2 = %f  <--\n", originalValue2);

//log
2012-07-07 22:20:20.737 [5887:19d03] datacellR2C2 as text --> 123456789 <---
2012-07-07 22:20:20.739 [5887:19d03] originalValue2 = 123456792.000000  <--
Example of [NSString floatValue]
//test
NSString *string = @"123,456,789";
float originalValue2 = [string  floatValue];
NSLog(@"datacellR2C2 as text --> %@ <---\n",string);  // this correctly shows the value in datacellR2C2
NSLog(@"originalValue2 = %f  <--\n", originalValue2);

//log
2012-07-07 22:16:15.913 [5709:19d03] datacellR2C2 as text --> 123,456,789 <---
2012-07-07 22:16:15.916 [5709:19d03] originalValue2 = 123.000000  <--