Reputation: 3
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];
}
Upvotes: 0
Views: 689
Reputation: 53000
This isn't an answer but an alternative, all those hasSuffix:
's just grated a bit. I think this does what you want:
float intpart;
float fracpart = modff(shoeSize, &intpart);
NSMutableString *appendedShoeSize;
int size = (int)intpart;
if (fracpart >= 0.3 && fracpart <= 0.6)
appendedShoeSize = [[NSMutableString alloc] initWithFormat:@"%d½", size];
else
{ if (fracpart > 0.6) size += 1;
appendedShoeSize = [[NSMutableString alloc] initWithFormat:@"%d", size];
}
modff
splits a float into its fractional (return value) and integer (via reference argument) parts. This code fragment doesn't require a mutable string, but I left that in as you might be doing other things with it later. The fragment also bumps .7 -> .9 to the next size.
Upvotes: 0
Reputation: 23359
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:
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 = [[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2] mutableCopy];
[appendedShoeSize appendString:@" ½"];
}
if ([appendedShoeSize hasSuffix:@".0"] || [appendedShoeSize hasSuffix:@".1"] || [appendedShoeSize hasSuffix:@".2"]) {
appendedShoeSize = [[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2] mutableCopy];
}
Upvotes: 3
Reputation: 15849
As Daniel pointed out substringToIndex:
returns a new string.
You should use replaceCharactersInRange:withString:
, for example:
NSRange range; range.location = [appendedShoeSize length] - 2; range.length = 2;
[appendedShoeSize replaceCharactersInRange:range withString:@" ½"]
More reference about NSMutableString methods can be found at http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html
Upvotes: 0
Reputation: 912
When you call substringToIndex:, it won't modify the existing string. It returns a result. You have to use something like : NSString * result = [appendedShoeSize substringToIndex:[appendedShoeSize length] - 2];
Upvotes: 0