iOS中NSString转float失真问题的解决办法


直接打印[@"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

调用方法是


需要示例工程的可以联系我


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值