回答

3

由於您使用的是%.2f格式說明,通過定義格式的浮點值,結果字符串將始終有兩位小數。如果someFloatValue是5,您將得到5.00。如果someFloatValue是3.1415926,你將得到3.14。

有沒有必要測試。對於給定的格式說明符,它將始終如此。

編輯:它發生在我身上,您可能確實想確認,實際上您正在使用正確的格式說明符。檢查結果字符串的一種方法是:

NSRange range = [strokeValue rangeOfString:@"."]; 
assert(range.location != NSNotFound && range.location == strokeValue.length - 3, @"String doesn't have two decimals places"); 
1
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\.[0-9]{2}$" options:0 error:nil]; 
if([regex numberOfMatchesInString:strokeValue options:0 range:NSMakeRange(0, [strokeValue length])]) { 
    // Passed 
} else { 
    // failed 
} 

(未經測試)

相關問題