开发中,碰到数值计算是很正常的,如果是整型计算都没问题,但是如果碰到浮点数(小数)不管是float double型,只要参与计算就会出现失真的情况,
iOS 开发中 可以使用NSDecimalNumber来计算和比较,今天整理了一下方法,用链式编程的思想 封装了一个 NSString 类目。目前只包含简单的加、减、乘、除、大于、小于、大于、等于,大于等于、小于等于的方法。
备注:本人的项目数据模型用的YYModel,后台返回的数值用NSNumber存,计算的时候用NSNumber的stringValue属性。
.h 文件
- (NSString *(^) (NSString *))add;
- (NSString *(^) (NSString *))sub;
- (NSString *(^) (NSString *))mul;
- (NSString *(^) (NSString *))div;
- (BOOL(^) (NSString*))dengyu;
- (BOOL(^) (NSString*))dayu;
- (BOOL(^) (NSString*))dayudengyu;
- (BOOL(^) (NSString*))xiaoyu;
- (BOOL(^) (NSString*))xiaoyudengyu;
.m 文件
- (NSString *(^) (NSString *))add {
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return^NSString*(NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSDecimalNumber*resultNum = [num1decimalNumberByAdding:num2];
return[resultNum stringValue];
};
}
- (NSString *(^)(NSString *))sub {
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return ^NSString* (NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSDecimalNumber *resultNum = [num1decimalNumberBySubtracting:num2];
return [resultNum stringValue];
};
}
- (NSString *(^)(NSString *))mul {
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return ^NSString* (NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSDecimalNumber*resultNum = [num1decimalNumberByMultiplyingBy:num2];
return [resultNum stringValue];
};
}
- (NSString *(^)(NSString *))div {
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return ^NSString* (NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSDecimalNumber*resultNum = [num1decimalNumberByDividingBy:num2];
return [resultNum stringValue];
};
}
- (BOOL(^) (NSString*))dengyu{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return ^BOOL(NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSComparisonResult result = [num1compare:num2];
if (result ==NSOrderedSame) {
returnYES;
} else {
returnNO;
}
};
}
- (BOOL(^)(NSString*))dayu{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return ^BOOL(NSString *value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSComparisonResult result = [num1compare:num2];
if (result == NSOrderedDescending) {
returnYES;
} else {
returnNO;
}
};
}
- (BOOL(^) (NSString*))dayudengyu{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return ^BOOL(NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSComparisonResult result = [num1compare:num2];
if(result == NSOrderedDescending || result ==NSOrderedSame) {
returnYES;
} else {
returnNO;
}
};
}
- (BOOL(^) (NSString*))xiaoyu{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return ^BOOL(NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSComparisonResult result = [num1compare:num2];
if (result ==NSOrderedAscending) {
returnYES;
} else {
returnNO;
}
};
}
- (BOOL(^) (NSString*))xiaoyudengyu{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];
return^BOOL(NSString*value) {
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:value];
NSComparisonResult result = [num1compare:num2];
if (result ==NSOrderedAscending || result ==NSOrderedSame) {
returnYES;
} else {
returnNO;
}
};
}
调用非常简单:因为返回的是字符串,在工程里都不用转换可以直接使用。
NSString *num1 = @"3.455";
NSString *rst = num1.add(@"4.7").sub(@"5.6789");