iOS处理服务器返回的金额的时候,有时候会有类似0.0100,有时候会有类似8.80000001样子,有时候传销7.9999999的时候。其实是iOS平台的解析的问题。
两种方法
1.NSString的一个类别方法
/** 字符串小数格式化
@return 小数字符串 */
- (NSString *)decimalNumberString {
NSString *numberString = [NSString stringWithFormat:@"%lf",[self doubleValue]];
return [NSString stringWithFormat:@"%@",[NSDecimalNumber decimalNumberWithString:numberString]];
}
2.转化一下
-(NSString *)stringByYouWantWithOriginString:(NSString *)originString{
NSString *MM = @"";
if ([originString containsString:@"."]) {
NSArray *array_XString = [originString componentsSeparatedByString:@"."];
NSString *string_XLast = array_XString.lastObject;
if (string_XLast.length == 1 ) {
MM = [NSString stringWithFormat:@"%.1f",[originString floatValue]];
}else if (string_XLast.length >=2){
MM = [NSString stringWithFormat:@"%.2f",[originString floatValue]];
}
if (string_XLast.length >=2){
if ([MM hasSuffix:@"00"]) {
MM = [NSString stringWithFormat:@"%.0f" , [MM floatValue]];
}else if ([MM hasSuffix:@"0"]){
MM = [NSString stringWithFormat:@"%.1f" , [MM floatValue]];
}
}else if (string_XLast.length ==1 ){
if ([MM hasSuffix:@"0"]) {
MM =[NSString stringWithFormat:@"%.0f" , [MM floatValue]];
}
}
}else{
MM =originString;
}
if ([MM hasSuffix:@".00"]) {
MM = [NSString stringWithFormat:@"%.0f",[MM floatValue]];
}
return MM;
}
网友评论