科学计数使用NSDecimal

注意:该文章在学习CorePlot框架时编写,仅供学习使用,以下为NSDecimal所有的方法及使用样例,及说明。

复制

  • NSDecimalCopy

比较

  • NSDecimalCompare

加减乘除 四舍五入

  • NSDecimalAdd
  • NSDecimalSubtract
  • NSDecimalMultiply
  • NSDecimalDivide
  • NSDecimalRound

N次方

  • NSDecimalPower

10的N次方

  • NSDecimalMultiplyByPowerOf10

转化成字符串

  • NSDecimalString

其他

  • NSDecimalCompact
  • NSDecimalIsNotANumber
  • NSDecimalNormalize

逻辑判断比较大小

NSDecimalCompare(比较大小)

//比较
FOUNDATION_EXPORT NSComparisonResult NSDecimalCompare(const NSDecimal *leftOperand, const NSDecimal *rightOperand);
   // NSDecimalCompare:Compares leftOperand and rightOperand.

示例代码:比较两个NSDecimal类型是否左侧大于右侧数值

BOOL CPTDecimalGreaterThan(NSDecimal leftOperand, NSDecimal rightOperand)
{
   return NSDecimalCompare(&leftOperand, &rightOperand) == NSOrderedDescending;
}

比较结果

typedef NS_ENUM(NSInteger, NSComparisonResult) {
 NSOrderedAscending = -1L, //升序排列
 NSOrderedSame, //相等
 NSOrderedDescending//降序排列
};

算数运算

NSDecimalAdd(加)

//加法
FOUNDATION_EXPORT NSCalculationError NSDecimalAdd(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);

示例代码:

NSDecimal CPTDecimalAdd(NSDecimal leftOperand, NSDecimal rightOperand)
{
    NSDecimal result;

    NSDecimalAdd(&result, &leftOperand, &rightOperand, NSRoundBankers);
    return result;
}

NSDecimalSubtract(减)

//减法
FOUNDATION_EXPORT NSCalculationError NSDecimalSubtract(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);

示例代码:

NSDecimal CPTDecimalSubtract(NSDecimal leftOperand, NSDecimal rightOperand)
{
    NSDecimal result;

    NSDecimalSubtract(&result, &leftOperand, &rightOperand, NSRoundBankers);
    return result;
}

NSDecimalMultiply(乘)

//乘法
FOUNDATION_EXPORT NSCalculationError NSDecimalMultiply(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);

示例代码:

NSDecimal CPTDecimalMultiply(NSDecimal leftOperand, NSDecimal rightOperand)
{
    NSDecimal result;

    NSDecimalMultiply(&result, &leftOperand, &rightOperand, NSRoundBankers);
    return result;
}

NSDecimalDivide(除)

FOUNDATION_EXPORT NSCalculationError NSDecimalDivide(NSDecimal *result, const NSDecimal *leftOperand, const NSDecimal *rightOperand, NSRoundingMode roundingMode);

示例代码:

NSDecimal CPTDecimalDivide(NSDecimal numerator, NSDecimal denominator)
{
    NSDecimal result;

    NSDecimalDivide(&result, &numerator, &denominator, NSRoundBankers);
    return result;
}

NSDecimalRound(四舍五入)

四舍五入NSRoundingMode模式

/***************    Type definitions        ***********/

// Rounding policies :舍入策略
// Original
//    value 1.2  1.21  1.25  1.35  1.27
// Plain    1.2  1.2   1.3   1.4   1.3
// Down     1.2  1.2   1.2   1.3   1.2
// Up       1.2  1.3   1.3   1.4   1.3
// Bankers  1.2  1.2   1.2   1.4   1.3
typedef NS_ENUM(NSUInteger, NSRoundingMode) {
    NSRoundPlain,   // Round up on a tie 用于四舍五入
    NSRoundDown,    // Always down == truncate只舍不入
    NSRoundUp,      // Always up不舍只入
    NSRoundBankers  //用于四舍五入,但是只有大于小数为6~9才进位
};

对应的方法:

//参数1:输出的结果
//参数2:输入的值
//参数3:保留到小数点后几位,传入NSInteger类型
//参数4:舍入策略
FOUNDATION_EXPORT void NSDecimalRound(NSDecimal *result, const NSDecimal *number, NSInteger scale, NSRoundingMode roundingMode);

测试示例代码:测试NSRoundBankers

 for (int i = 0 ; i<10; i++) {
        NSString *str =[NSString stringWithFormat:@"1.22%d",i];
        NSLog(@"小数值:%@\n",str);
        NSDecimal result;
        NSScanner *theScanner = [[NSScanner alloc] initWithString:str];
                
       [theScanner scanDecimal:&result];
       NSDecimal coord = result;
                
        NSDecimalRound(&coord, &coord, 2, NSRoundBankers);
        CGFloat coordFloat = [[NSDecimalNumber decimalNumberWithDecimal: coord] doubleValue];
                NSLog(@"结   果:%f",coordFloat);
        }

执行结果:
小数值:1.220
结 果:1.220000
小数值:1.221
结 果:1.220000
小数值:1.222
结 果:1.220000
小数值:1.223
结 果:1.220000
小数值:1.224
结 果:1.220000
小数值:1.225
结 果:1.220000
小数值:1.226
结 果:1.230000
小数值:1.227
结 果:1.230000
小数值:1.228
结 果:1.230000
小数值:1.229
结 果:1.230000

NSDecimalPower(N次方)

//参数1:输出的结果
//参数2:输入的值
//参数3:N次方,传入NSInteger类型
//参数4:舍入策略,这个参数对输出结果有用,但不知道为啥结果是小数点后6位数
FOUNDATION_EXPORT NSCalculationError NSDecimalPower(NSDecimal *result, const NSDecimal *number, NSUInteger power, NSRoundingMode roundingMode);

示例代码:

  for (int i = 0 ; i<2; i++) {
                NSString *str =[NSString stringWithFormat:@"1.22%d",i];
                NSLog(@"小数值:%@\n",str);
                NSDecimal result;
                NSScanner *theScanner = [[NSScanner alloc] initWithString:str];
                
                [theScanner scanDecimal:&result];
                NSDecimal coord;
                
                NSDecimalCopy(&coord,&result);
                
                NSDecimalPower(&coord, &coord, 3, NSRoundPlain);
                CGFloat coordFloat =  [[NSDecimalNumber decimalNumberWithDecimal: coord] doubleValue];
                NSLog(@"结  果:%f",coordFloat);
            }

输出结果:
小数值:1.220
结 果:1.815848
小数值:1.221
结 果:1.820317

NSDecimalMultiplyByPowerOf10(10的N次方倍)

  for (int i = 0 ; i<2; i++) {
                NSString *str =[NSString stringWithFormat:@"1.22%d",i];
                NSLog(@"小数值:%@\n",str);
                NSDecimal result;
                NSScanner *theScanner = [[NSScanner alloc] initWithString:str];
                
                [theScanner scanDecimal:&result];
                NSDecimal coord;
                
                NSDecimalCopy(&coord,&result);
                
                NSDecimalMultiplyByPowerOf10(&coord, &coord, 4, NSRoundPlain);
                CGFloat coordFloat =  [[NSDecimalNumber decimalNumberWithDecimal: coord] doubleValue];
                NSLog(@"结  果:%f",coordFloat);
            }

输出结果:
小数值:1.220
结 果:12200.000000
小数值:1.221
结 果:12210.000000

NSDecimalString(转化成字符串)

//参数1:要转化的NSDecimal值
//参数2:选择转化的国家语言
FOUNDATION_EXPORT NSString *NSDecimalString(const NSDecimal *dcm, id __nullable locale);

代码示例一:法文输出

     for (int i = 0 ; i<2; i++) {
                NSString *str =[NSString stringWithFormat:@"1.22%d",i];
                NSLog(@"小数值:%@\n",str);
                NSDecimal result;
                NSScanner *theScanner = [[NSScanner alloc] initWithString:str];
                
                [theScanner scanDecimal:&result];

                NSDecimal coord;
                NSDecimalCopy(&coord,&result);

                NSLocale *locale =[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
                NSLog(@"%@",NSDecimalString(&coord, locale));

            }

输出结果:
小数值:1.220
1,22
小数值:1.221
1,221

代码示例二:中国语言

//只需替换该句代码即可
 NSLocale *locale =[NSLocale currentLocale];

输出结果:
小数值:1.220
1.22 //有“,”号变为“.”号
小数值:1.221
1.221

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,871评论 6 13
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,625评论 25 709
  • 不知不觉做开发已经差不多一年了,在这段说长不长说短不短的时间里自己心里还有有许许多多的感觉,今天有必要写这篇文章,...
    Zhangyuting193阅读 4,417评论 18 12
  • 独抱琵琶,无言有恨千千绪。天涯苦旅,莫问家何处。 切切嘈嘈,都向幺弦诉。蚕丛路,莺飞燕舞,片片桃花雨。
    朗读者_LDZ阅读 2,713评论 1 5
  • 图文/anMoo韩魔 这里讲的是一只又胖又色的鸟的故事。 咳...咳...,我说的是彩色的色! 今天来了一只小肥鸟...
    anMoo韩魔阅读 4,900评论 3 12