我想使這個程序,以便有一個小數的唯一時間是當小數是.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];
}
當然,我們可以在NSMutableString上使用'deleteCharactersInRange'來截斷它。 – 2012-01-08 21:29:20
這將是一個更一致的使用NSMutableString在這種情況下 – Daniel 2012-01-08 21:30:52
當我嘗試這個我得到了一個警告:「不兼容的指針類型分配給'NSMutableString * __強'從'NSString *' – AppleGuy 2012-01-08 21:36:15