‘NSDecimalNumber--十进制数’使用方法

96
Yasin的简书
2016.02.24 11:55* 字数 185

NSDecimalNumber-十进制数,可以有小数点,可以是正负数。继承于NSNumber


  • + (NSDecimalNumber *)decimalNumberWithMantissa:(unsigned long long)mantissa exponent:(short)exponent isNegative:(BOOL)flag;
    mantissa:长整形;exponent:指数;flag:正负数。
NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:-2 isNegative:NO];   //12.75
    subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:2 isNegative:YES];   //-127500
  • + (NSDecimalNumber *)decimalNumberWithDecimal:(NSDecimal)dcm;
    将C语言NSDecimal类型转成一个十进制数。
    NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:-2 isNegative:NO];   //12.75
    NSDecimal decimalValue = [subtotalAmount decimalValue];
    /**
     *  数据结构:
     (NSDecimal) decimalValue = {
     _exponent = -2
     _length = 1
     _isNegative = 0
     _isCompact = 1
     _reserved = 0
     _mantissa = ([0] = 1275, [1] = 0, [2] = 0, [3] = 0, [4] = 0, [5] = 0, [6] = 0, [7] = 0)
    }
     */
    subtotalAmount = [NSDecimalNumber decimalNumberWithDecimal:decimalValue];   //12.75
  • + (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue;
    将字符串转成一个十进制数。
NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"-12.74"];      //-12.74
    discountAmount = [NSDecimalNumber decimalNumberWithString:@"127.4"];      //127.4
  • + (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue locale:(nullable id)locale;
    这个有点复杂,locale代表一种格式,就像date的格式化一样。这里的locale可以传递两种格式
    NSDictionary类型:
NSDictionary *locale = [NSDictionary dictionaryWithObject:@"," forKey:NSLocaleDecimalSeparator];    //以","当做小数点格式
NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"123,40" locale:locale];    //123.4
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];    //法国数据格式,法国的小数点是','逗号
NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"123,40" locale:locale];    //123.4
+(NSDecimalNumber *)zero; //0
+(NSDecimalNumber *)one; //1
+(NSDecimalNumber *)minimumDecimalNumber;
//-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+(NSDecimalNumber *)maximumDecimalNumber;
//3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+(NSDecimalNumber *)notANumber;
//非数字,常用于对比,比如:
[[NSDecimalNumber notANumber] isEqualToNumber:myNumber];
  • 加法运算
-(NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
-(NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
  • 减法运算
-(NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
-(NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
  • 乘法运算
-(NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
  • 除法运算
-(NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
-(NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
  • a的n次方
-(NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power;
-(NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
  • 指数运算
-(NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power;
-(NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power 
withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
  • 四舍五入运算
-(NSDecimalNumber *)decimalNumberByRoundingAccordingToBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
  • 比较运算
-(NSComparisonResult)compare:(NSNumber *)decimalNumber;

示例

NSDecimalNumber *discount1 = [NSDecimalNumber decimalNumberWithString:@"1.2"];
    NSDecimalNumber *discount2 = [NSDecimalNumber decimalNumberWithString:@"1.3"];
    NSComparisonResult result = [discount1 compare:discount2];
    if (result == NSOrderedAscending) {
        NSLog(@"1.2 < 1.3");
    } else if (result == NSOrderedSame) {
        NSLog(@"1.2 == 1.3");
    } else if (result == NSOrderedDescending) {
        NSLog(@"1.2 > 1.3");
    }
iOS
Web note ad 1