I have a string percentage in an array that I'd like to convert to a double. Obviously I have to both remove the percentage and then do something along the lines 开发者_开发百科of [string doubleValue]
. What is the most efficient way of removing the %? The action is repeated often so the method must be somewhat resource conservative.
As long as your string begins with a valid text representation of a floating-point number (it can only contain whitespace at the beginning), you can call doubleValue
and omit any preprocessing of the string.
NSLog(@"value: %f", [@" 95.5%" doubleValue]); // --> value: 95.500000
If the percentage sign is always the last symbol, would [string substringToIndex:string.length-1]
suffice?
精彩评论