美文网首页
NSDecimalNumber 简单使用(thuan)

NSDecimalNumber 简单使用(thuan)

作者: Leo_5cdb | 来源:发表于2020-05-11 09:59 被阅读0次

    由于项目是和金钱有关的,要求比较高,不能有精度丢失。用NSString的DoubleValue方法并不准确,金额较大会出现精度丢失的问题。

    这里我推荐直接转换为NSDecimalNumber来处理,保证精度不丢失。

    NSDecimalNumber介绍

    NSDecimalNumber是NSNumber的子类,封装了一系列运算的接口。方便我们直接调用api进行高精度数字的加减乘除,幂运算,四舍五入等。NSDecimalNumber可以直接通过NSString类型构建,不用转为Double类型。

    最常用的场景就是输入框,用户输了一个比较大的数字,需要我们处理,如果此时转为Double,精度无法保证,如果用NSDecimalNumber,就可以直接用NSString构建,直接运算,防止精度丢失。

    不过,NSDecimalNumber,运算时间相对要长一点。

    具体使用方法,为了以后方便使用,封装了一个Category,我把源码放上来。

    NSString+calculate.h

    #import <Foundation/Foundation.h>

    @interface NSString (calculate)

    //加

    - (NSString *)calculateByAdding:(NSString *)stringNumer;

    //减

    - (NSString *)calculateBySubtracting:(NSString *)stringNumer;

    //乘

    - (NSString *)calculateByMultiplying:(NSString *)stringNumer;

    //除

    - (NSString *)calculateByDividing:(NSString *)stringNumer;

    //幂运算

    - (NSString *)calculateByRaising:(NSUInteger)power;

    //四舍五入

    - (NSString *)calculateByRounding:(NSUInteger)scale;

    //是否相等

    - (BOOL)calculateIsEqual:(NSString *)stringNumer;

    //是否大于

    - (BOOL)calculateIsGreaterThan:(NSString *)stringNumer;

    //是否小于

    - (BOOL)calculateIsLessThan:(NSString *)stringNumer;

    //转成小数

    - (double)calculateDoubleValue;

    @end

    NSString+calculate.m

    #import "NSString+calculate.h"

    @implementation NSString (calculate)

    //加

    - (NSString *)calculateByAdding:(NSString *)stringNumer

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:stringNumer];

        NSDecimalNumber *addingNum = [num1 decimalNumberByAdding:num2];

        return [addingNum stringValue];

    }

    //减

    - (NSString *)calculateBySubtracting:(NSString *)stringNumer

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:stringNumer];

        NSDecimalNumber *subtractingNum = [num1 decimalNumberBySubtracting:num2];

        return [subtractingNum stringValue];

    }

    //乘

    - (NSString *)calculateByMultiplying:(NSString *)stringNumer

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:stringNumer];

        NSDecimalNumber *multiplyingNum = [num1 decimalNumberByMultiplyingBy:num2];

        return [multiplyingNum stringValue];

    }

    //除

    - (NSString *)calculateByDividing:(NSString *)stringNumer

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:stringNumer];

        NSDecimalNumber *dividingNum = [num1 decimalNumberByDividingBy:num2];

        return [dividingNum stringValue];

    }

    //幂运算

    - (NSString *)calculateByRaising:(NSUInteger)power

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *raisingNum = [num1 decimalNumberByRaisingToPower:power];

        return [raisingNum stringValue];

    }

    //四舍五入

    - (NSString *)calculateByRounding:(NSUInteger)scale

    {

        NSDecimalNumberHandler * handler = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:scale raiseOnExactness:NO raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *roundingNum = [num1 decimalNumberByRoundingAccordingToBehavior:handler];

        return [roundingNum stringValue];

    }

    //是否相等

    - (BOOL)calculateIsEqual:(NSString *)stringNumer

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:stringNumer];

        NSComparisonResult result = [num1 compare:num2];

        if (result == NSOrderedSame) {

            return YES;

        }

        return NO;

    }

    //是否大于

    - (BOOL)calculateIsGreaterThan:(NSString *)stringNumer

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:stringNumer];

        NSComparisonResult result = [num1 compare:num2];

        if (result == NSOrderedDescending) {

            return YES;

        }

        return NO;

    }

    //是否小于

    - (BOOL)calculateIsLessThan:(NSString *)stringNumer

    {

        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:self];

        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:stringNumer];

        NSComparisonResult result = [num1 compare:num2];

        if (result == NSOrderedAscending) {

            return YES;

        }

        return NO;

    }

    - (double)calculateDoubleValue

    {

        NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:self];

        return [num doubleValue];

    }

    @end

    相关文章

      网友评论

          本文标题:NSDecimalNumber 简单使用(thuan)

          本文链接:https://www.haomeiwen.com/subject/nhvynhtx.html