直接打印[@"21.2" floatValue],得到的结果是
————————————————————————————
————————————————————————————
百度上搜了很多,有的说用doubleValue替换floatValue,要是从根本上解决问题还得使用NSDecimalNumber
写了一个NSString的分类来方便运算,这样传入传出的数值都是NSString类型
分别是
1 A+B
2 A-B
3 A*B
4 A/B
5 A>B
6A==B
7 A<B
.h
//
// NSString+DecimalNumber.h
// CEB
//
// Created by yfc on 17/5/9.
//
//
#import <Foundation/Foundation.h>
//
//解决str转float精度失真问题
//
@interface NSString(NSString_DecimalNumber)
+(NSString *)A:(NSString *)a jiaB:(NSString *)b;
+(NSString *)A:(NSString *)a jianB:(NSString *)b;
+(NSString *)A:(NSString *)a chengyiB:(NSString *)b;
+(NSString *)A:(NSString *)a chuyiB:(NSString *)b;
+(BOOL)A:(NSString *)a dayuB:(NSString *)b;
+(BOOL)A:(NSString *)a dengyuB:(NSString *)b;
+(BOOL)A:(NSString *)a xiaoyuB:(NSString *)b;
@end
.m
//
// NSString+DecimalNumber.m
// CEB
//
// Created by yfc on 17/5/9.
//
//
#import "NSString+DecimalNumber.h"
@implementation NSString(NSString_DecimalNumber)
+(NSString *)A:(NSString *)a jiaB:(NSString *)b;{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:a];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:b];
NSDecimalNumber *resultNum = [num1 decimalNumberByAdding:num2];
return [resultNum stringValue];
}
+(NSString *)A:(NSString *)a jianB:(NSString *)b;{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:a];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:b];
NSDecimalNumber *resultNum = [num1 decimalNumberBySubtracting:num2];
return [resultNum stringValue];
}
+(NSString *)A:(NSString *)a chengyiB:(NSString *)b;{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:a];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:b];
NSDecimalNumber *resultNum = [num1 decimalNumberByMultiplyingBy:num2];
return [resultNum stringValue];
}
+(NSString *)A:(NSString *)a chuyiB:(NSString *)b;{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:a];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:b];
NSDecimalNumber *resultNum = [num1 decimalNumberByDividingBy:num2];
return [resultNum stringValue];
}
+(BOOL)A:(NSString *)a dayuB:(NSString *)b;{
NSDecimalNumber *discount1 = [NSDecimalNumber decimalNumberWithString:a];
NSDecimalNumber *discount2 = [NSDecimalNumber decimalNumberWithString:b];
NSComparisonResult result = [discount1 compare:discount2];
if (result == NSOrderedAscending) {
return NO;
} else if (result == NSOrderedSame) {
return NO;
} else if (result == NSOrderedDescending) {
return YES;
}
return NO;
}
+(BOOL)A:(NSString *)a dengyuB:(NSString *)b;{
NSDecimalNumber *discount1 = [NSDecimalNumber decimalNumberWithString:a];
NSDecimalNumber *discount2 = [NSDecimalNumber decimalNumberWithString:b];
NSComparisonResult result = [discount1 compare:discount2];
if (result == NSOrderedAscending) {
return NO;
} else if (result == NSOrderedSame) {
return YES;
} else if (result == NSOrderedDescending) {
return NO;
}
return NO;
}
+(BOOL)A:(NSString *)a xiaoyuB:(NSString *)b;{
NSDecimalNumber *discount1 = [NSDecimalNumber decimalNumberWithString:a];
NSDecimalNumber *discount2 = [NSDecimalNumber decimalNumberWithString:b];
NSComparisonResult result = [discount1 compare:discount2];
if (result == NSOrderedAscending) {
return YES;
} else if (result == NSOrderedSame) {
return NO;
} else if (result == NSOrderedDescending) {
return NO;
}
return NO;
}
@end
调用方法是
需要示例工程的可以联系我