I have a text field in an xib file. In a method in the .m file, I can print the contents of the text field, but I cannot get those contents converted to a float. The text field is formatted with commas, as in 123,456,789. Below is the code snippet, where datacellR2C2 is the textfield.
float originalValue2 = originalValue2 = [datacellR2C2.text floatValue];
NSLog(@"datacellR2C2 as text --> %@ <---\n",datacellR2C2.text); // this correctly shows the value in datacellR2C2
NSLog(@"originalValue2 = %f <--\n", originalValue2); // this incorrectly returns the value 1.0
I would appreciate any suggestions for a fix or a direction where I should look for the problem.
In the declaration for
-floatValuea comment is shown:Ergo, commas cause truncation, because they are trailing chars. Even the string you provided (123,456,789) only prints 123.000, because that’s all
-floatValuesees.Just get rid of them with a simple
+stringByReplacingOccurrencesOfString:withString:, and remove those trailing commas:By the way, a float is rounding that string up to an even number, use double precision instead.