1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > IOS字符串截取保留小数点后两位

IOS字符串截取保留小数点后两位

时间:2022-05-22 02:26:34

相关推荐

IOS字符串截取保留小数点后两位

-(NSString*)getTheCorrectNum:(NSString*)tempString{//计算截取的长度NSUInteger endLength = tempString.length;//判断字符串是否包含 .if ([tempString containsString:@"."]) {//取得 . 的位置NSRange pointRange = [tempString rangeOfString:@"."];NSLog(@"%lu",pointRange.location);//判断 . 后面有几位NSUInteger f = tempString.length - 1 - pointRange.location;//如果大于2位就截取字符串保留两位,如果小于两位,直接截取if (f > 2) {endLength = pointRange.location + 2;}}//先将tempString转换成char型数组NSUInteger start = 0;const char *tempChar = [tempString UTF8String];//遍历,去除取得第一位不是0的位置for (int i = 0; i < tempString.length; i++) {if (tempChar[i] == '0') {start++;}else {break;}}//根据最终的开始位置,计算长度,并截取NSRange range = {start,endLength-start};tempString = [tempString substringWithRange:range];return tempString;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。