iOS中关于如何正确处理价格

Posted PinkJoker

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS中关于如何正确处理价格相关的知识,希望对你有一定的参考价值。

NSDecimalNumber
NSDecimalNumber是ios系统的api,是一种精确计算。主要解决一下问题:
1、字符串转float等不精确问题。
2、精确计算
3、保留小数位数
4、四舍五入及其它的保留小数位数规则
NSDecimalNumber的运算

 //1、字符串 -> NSDecimalNumber
    NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:@"123"];
    NSLog(@"num1 == %@",num1);
    //2、NSNumber -> NSDecimalNumber(基础类型的话,先转成NSNumber)
    NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithInt:123] decimalValue]];
    NSLog(@"num2 == %@",num2);
//相关运算


/*加 +
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
 */

NSDecimalNumber *num3 = [num1 decimalNumberByAdding:num2];
NSLog(@"num3 == %@",num3);


/*减 -
 - (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
 - (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
 */
NSDecimalNumber *num4 = [num1 decimalNumberBySubtracting:num2];
NSLog(@"num4 == %@",num4);

/*乘 *
 - (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber;
 - (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
 */
NSDecimalNumber *num5 = [num1 decimalNumberByMultiplyingBy:num2];
NSLog(@"num5 == %@",num5);

/*除 \\
 - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
 - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
 */
NSDecimalNumber *num6 = [num1 decimalNumberByDividingBy:num2];
NSLog(@"num6 == %@",num6);

/*比较 
 - (NSComparisonResult)compare:(NSNumber *)decimalNumber;
 */

NSComparisonResult result = [num1 compare:num2];
NSLog(@"result == %ld",(long)result);


//保留小数及相关参数含义
/*保留小数
 + (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;
 */

//1、 NSRoundingMode

// 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  // on a tie round so last digit is even 貌似是:
       if(四舍五入位==5if(5后有非0数字)//如:1.251  - 1.26else//如:1.25 - 1.25else
            四舍五入
        

    ;
*/
//2、scale 保留小数个数
//3、其它:溢出等是否报错

NSDecimalNumber *num_1 = [NSDecimalNumber decimalNumberWithString:@"1.2"];
NSDecimalNumber *num_2 = [NSDecimalNumber decimalNumberWithString:@"3.4"];
NSDecimalNumberHandler *handel = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

NSDecimalNumber *num_3 = [num_1 decimalNumberByDividingBy:num_2 withBehavior:handel];
NSLog(@"num_3 == %@",num_3);

//打印结果
/*
2015-12-11 13:26:28.269 test[32280:9906455] num1 == 123
2015-12-11 13:26:28.269 test[32280:9906455] num2 == 123
2015-12-11 13:26:28.269 test[32280:9906455] num3 == 246
2015-12-11 13:26:28.270 test[32280:9906455] num4 == 0
2015-12-11 13:26:28.270 test[32280:9906455] num5 == 15129
2015-12-11 13:26:28.270 test[32280:9906455] num6 == 1
2015-12-11 13:26:28.270 test[32280:9906455] result == 0
2015-12-11 13:26:28.270 test[32280:9906455] num_3 == 0.35
*/

以上是关于iOS中关于如何正确处理价格的主要内容,如果未能解决你的问题,请参考以下文章

JDK中关于BIO,NIO,AIO,同步,异步介绍

众包中关于DS模型及其扩展设定总结

iOS 设计中关于UIScrollViewDelegate的几个代理方法的简单介绍

带你整理面试过程中关于 Java 中的 异常分类及处理的相关知识

iOS中关于字符 “&”的作用?

iOS开发中关于多线程的问题(NSOperationQueue,NSThread,GCD)