开发者

Convert string percentage to double

开发者 https://www.devze.com 2023-04-02 10:54 出处:网络
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

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消