美文网首页
iOS开发中浮点型数据的四舍五入

iOS开发中浮点型数据的四舍五入

作者: Michael_NO1 | 来源:发表于2017-08-04 11:05 被阅读132次

    方法一

        CGFloat fl1 = 1.23;
        CGFloat fl2 = 1.34;
        CGFloat fl3 = 1.35;
        CGFloat fl4 = 1.36;
        CGFloat fl5 = 1.44;
        CGFloat fl6 = 1.45;
        CGFloat fl7 = 1.46;
        
        NSLog(@"fl1 = %.1f, fl2 = %.1f, fl3 = %.1f, fl4 = %.1f, fl5 = %.1f, fl6 = %.1f, fl7 = %.1f",fl1,fl2,fl3,fl4,fl5,fl6,fl7);
    
    打印结果:
    fl1 = 1.2, fl2 = 1.3, fl3 = 1.4, fl4 = 1.4, fl5 = 1.4, fl6 = 1.4, fl7 = 1.5
    

    从上边的打印结果可以看出当前面为1.3的时候最后一位为四舍五入, 当前面为1.4的时候最后一位为五舍六入, 这是因为该机制为当要保留的最后一位数为奇数时会采用四舍五入, 当要保留的最后一位为偶数时采用五舍六入;
    如果想要更多的舍入精度可以采用以下方法二

    方法二

    这个方法涉及到两个类:NSDecimalNumber & NSDecimalNumberHandler

    // 四舍五入涉及到的NSDecimalNumberHandler
    // 类方法: + (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;
    /*
    参数讲解:
    RoundingMode  四舍五入的标准.
    原始值: 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
        NSRoundPlain,   // Round up on a tie    严格遵守四舍五入
        NSRoundDown,    // Always down == truncate    直接舍去
        NSRoundUp,      // Always up                                 直接进一位
        NSRoundBankers  // on a tie round so last digit is even   进位标准同方法一
    
         scale : 需要保留的精度。
         raiseOnExactness : 为YES时在处理精确时如果有错误,就会抛出异常。
         raiseOnOverflow  : YES时在计算精度向上溢出时会抛出异常,否则返回。
         raiseOnUnderflow : YES时在计算精度向下溢出时会抛出异常,否则返回.
         raiseOnDivideByZero : YES时。当除以0时会抛出异常,否则返回。
    */
        NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:@"1.34"];
        NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:@"1.35"];
        NSDecimalNumber *num3 = [NSDecimalNumber decimalNumberWithString:@"1.44"];
        NSDecimalNumber *num4 = [NSDecimalNumber decimalNumberWithString:@"1.45"];
        NSDecimalNumberHandler *numHandler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
        
        NSString *str1 = [[num1 decimalNumberByRoundingAccordingToBehavior:numHandler] stringValue];
        NSString *str2 = [[num2 decimalNumberByRoundingAccordingToBehavior:numHandler] stringValue];
        NSString *str3 = [[num3 decimalNumberByRoundingAccordingToBehavior:numHandler] stringValue];
        NSString *str4 = [[num4 decimalNumberByRoundingAccordingToBehavior:numHandler] stringValue];
        NSLog(@"\n%@,%@",str1,str2);
        NSLog(@"\n%@,%@",str3,str4);
    打印结果:
    str1 = 1.3,str2 = 1.4
    str3 = 1.4,str4 = 1.4
    
    补充:
    NSDecimalNumber 同样可以进行加减乘除运算:
    - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
    - (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
    - (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber;
    - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
    

    如有错误欢迎批评指正.
    更多详细内容可参考苹果官方文档:
    https://developer.apple.com/reference/foundation/nsdecimalnumber

    相关文章

      网友评论

          本文标题:iOS开发中浮点型数据的四舍五入

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