微信和支付宝支付的金额不能是超过两位小数。
可以把后台超过两位小数的支付字符串修正为两位小数
-(NSString *)modifyWithStr:(NSString *)str { if(isCommonUnitEmptyString(str)) { return @""; } NSInteger flag = 0;//0开始,没有找到值;1找到值但是没有找全值;2找全值,在找剩余右边的字符串 NSString *actualAmountStr = @""; NSString *actualAmountValueStr = @""; NSString *rightStr = @""; NSString *newStr = @""; if(!isCommonUnitEmpty(str) && str.length > 8) { for(NSInteger n =0; n < str.length; n++) { NSString *str1 = [str substringWithRange:NSMakeRange(n, 1)]; if(!isCommonUnitEmptyString(str1)) { if(0 == flag) { if([str1 isEqualToString:@"="]) { flag = 1; } actualAmountStr = [NSString stringWithFormat:@"%@%@", actualAmountStr, str1]; } else if(1 == flag) { if([str1 isEqualToString:@"&"]) { rightStr = @"&"; flag = 2; } else { actualAmountValueStr = [NSString stringWithFormat:@"%@%@", actualAmountValueStr, str1]; } } else { rightStr = [NSString stringWithFormat:@"%@%@", rightStr, str1]; } } else { break; } } if(2 ==flag) { CGFloat actualAmountValue = [actualAmountValueStr floatValue] +0.005; newStr = [NSString stringWithFormat:@"%@%.2f%@", actualAmountStr, actualAmountValue,rightStr]; } } return newStr; } //判断是否为空 static inline BOOL isCommonUnitEmpty(id thing) { return thing == nil || ([thing isEqual:[NSNull null]]) || ([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) || [([NSString stringWithFormat:@"%@", thing]) isEqualToString:@"<null>"] || ([thing respondsToSelector:@selector(count)] && [(NSArray *)thing count] == 0); }
测试代码片段
NSString *str = @"?actualAmount=0.009&amount=0.01&id=2185"; str = [self modifyWithStr:str];