回答

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 
} 

(未经测试)

相关问题