I am trying to make this program so that the only time that there is a decimal is when the decimal is .5. I have been trying to use [string substringToIndex:[string length] - 2] but it does nothing. Is is because it cannot append the float?
float inchesInField = [sizeField.text floatValue];
float shoeSize = inchesInField * 3 - 22;
NSMutableString *appendedShoeSize = [[NSMutableString alloc]
initWithFormat:@"%.1f", shoeSize];
if ([appendedShoeSize hasSuffix:@".3"] || [appendedShoeSize hasSuffix:@".5"] ||
[appendedShoeSize hasSuffix:@".4"] || [appendedShoeSize hasSuffix:@".6"])
{
[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2];
[appendedShoeSize appendString:@" ½"];
}
if ([appendedShoeSize hasSuffix:@".0"] || [appendedShoeSize hasSuffix:@".1"] ||
[appendedShoeSize hasSuffix:@".2"])
{
[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2];
}
it is because substringToIndex: method of NSString returns the new string, it doesn’t modify the original string. appendString: is fine, but substringToIndex: is a method of NSString so it will not edit the original string.
This should do it: