2012-01-08 125 views
0

我想使這個程序,以便有一個小數的唯一時間是當小數是.5。我一直在嘗試使用[string substringToIndex:[string length] - 2],但它什麼也沒做。是因爲它不能附加浮點數?不能縮短NSString長度

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]; 
} 

回答

3

這是因爲substringToIndex:NSString方法返回新的字符串,它不會修改原始字符串。 appendString:很好,但substringToIndex:是NSString的一種方法,所以它不會編輯原始字符串。

這應做到:

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]; 
} 
+1

當然,我們可以在NSMutableString上使用'deleteCharactersInRange'來截斷它。 – 2012-01-08 21:29:20

+0

這將是一個更一致的使用NSMutableString在這種情況下 – Daniel 2012-01-08 21:30:52

+0

當我嘗試這個我得到了一個警告:「不兼容的指針類型分配給'NSMutableString * __強'從'NSString *' – AppleGuy 2012-01-08 21:36:15

0

當你調用substringToIndex :,它不會修改現有的字符串。它返回一個結果。你必須使用類似於: NSString * result = [attachedShoeSize substringToIndex:[attachedShoeSize length] - 2];

0

正如Daniel指出substringToIndex:返回一個新的字符串。

您應該使用replaceCharactersInRange:withString:,例如:

NSRange range; range.location = [appendedShoeSize length] - 2; range.length = 2; 
[appendedShoeSize replaceCharactersInRange:range withString:@" ½"] 

有關的NSMutableString方法的更多參考可在http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html

0

發現這不是一個答案,但替代,所有這些hasSuffix:的只是碎一點點。我想這你想要做什麼:

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分割一個浮動到它的分數(返回值)和整數(通過引用參數)部分。這段代碼片段不需要可​​變字符串,但我留下了它,因爲稍後您可能正在做其他事情。該片段還會使.7 - > .9變爲下一個大小。