亲宝软件园·资讯

展开

iOS double/float 精度 详解iOS之关于double/float数据计算精度问题

叫我大表弟吧 人气:5
想了解详解iOS之关于double/float数据计算精度问题的相关内容吗,叫我大表弟吧在本文为您仔细讲解iOS double/float 精度的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:iOS,double,float,精度,iOS,精度,下面大家一起来学习吧。

1、我们的app有一个判断,当用户输入的值,小于等于剩余的余额时,给用户弹窗,代码不往下执行

当用户输入 0.01 时,po 一下的结果是:0.10000.... ,当用户的余额是0.01时,po一下网络请求的返回结果是:0.009999...

当用户输入 66.05时,po  一下的结果是:66.049999...,当用户的余额是66.05时,po一下网络请求的返回结果是:66.049999...

所以,当数据较大时,不会有影响,因为用户输入的数据和网络返回数据都被变成了不精确的小值了;但是,当数据较小时,用户输入的数据没有被转,网络返回的数据被转成了不精确的小值,导致用户输入永远大于网络返回的数据,代码无法往下执行;

解决:使用 NSDecimalNumber 将float、double数据转化成NSDecimalNumber 类型的对象进行 +、-、*、/计算,再取值。

- (double)DecimalNumber:(double)num1 num2:(double)num2 { 
   
  NSDecimalNumber *n1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",num1]]; 
   
  NSDecimalNumber *n2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",num2]]; 
   
  NSDecimalNumber *n3 = [n1 decimalNumberBySubtracting:n2]; 
   
  return n3.doubleValue; 
} 
// 调用: 
double result = [self DecimalNumber: 2.01 num2: 2]; // 结果为 0.01 

其中:
decimalNumberBySubtracting: n1 - n2,返回 n1 - n2 的结果值
decimalNumberByMultiplyingBy:n1 * n2,
decimalNumberByDividingBy:n1 / n2,
decimalNumberByAdding:n1 + n2,
compare:比较 n1 和 n2,返回比较结果;

2、进行数据的四舍五入获得结果 

- (NSString*)Rounding:(float)number afterPoint:(NSInteger)position 
{ 
  NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode: NSRoundPlain scale: position raiseOnExactness: NO raiseOnOverflow: NO raiseOnUnderflow:NO raiseOnDivideByZero: NO]; 
   
  NSDecimalNumber *floatDecimal = [[NSDecimalNumber alloc] initWithFloat: number]; 
   
  NSDecimalNumber *resultNumber = [floatDecimal decimalNumberByRoundingAccordingToBehavior:handler]; 
   
  return [NSString stringWithFormat:@"%@",resultNumber]; 
} 
// 调用: 
NSString *result = [self Rounding:8.00092 afterPoint:3]; // 结果为 8.001 

其中,参数number是需要四舍五入的数据,position是小数点后保留的位数;

高精度要求

使用系统提供的NSDecimalNumber API进行计算,最后转换为字符串输出显示。NSDecimalNumber转换方法:

#import "NSDecimalNumber+Y_Add.h"

@implementation NSDecimalNumber (Y_Add)

+ (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value{
  
  return [self y_decimalNumberWithFloat:value scale:2];
}

+ (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value scale:(short)scale{
  
  return [self y_decimalNumberWithFloat:value roundingMode:NSRoundBankers scale:scale];
}

+ (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value roundingMode:(NSRoundingMode)roundingMode scale:(short)scale{
  
  return [[[NSDecimalNumber alloc] initWithFloat:value] y_decimalNumberHandlerWithRoundingMode:roundingMode scale:scale];
}

+ (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value{
  
  return [self y_decimalNumberWithDouble:value scale:2];
}

+ (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value scale:(short)scale{
  
  return [self y_decimalNumberWithDouble:value roundingMode:NSRoundBankers scale:scale];
}

+ (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value roundingMode:(NSRoundingMode)roundingMode scale:(short)scale{
  
  return [[[NSDecimalNumber alloc] initWithFloat:value] y_decimalNumberHandlerWithRoundingMode:roundingMode scale:scale];
}
 
/**
 * <#Description#>
 *
 * @return <#return value description#>
 */
- (NSDecimalNumber *)y_decimalNumberHandler{
  
  return [self y_decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:2];
}

- (NSDecimalNumber *)y_decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale{
   NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:roundingMode
                                               scale:scale
                                         raiseOnExactness:NO
                                          raiseOnOverflow:YES
                                         raiseOnUnderflow:YES
                                        raiseOnDivideByZero:YES];
  return [self decimalNumberByRoundingAccordingToBehavior:handler];
}
@end

普通精度要求

使用系统提供的数学运算进行计算,最后转换为字符串输出显示。保留小数点后N位的方法:

/**
 * formatterNumber .00 小数点后两位
 *
 * @param number <#number description#>
 *
 * @return <#return value description#>
 */
+ (NSString *)y_formatterNumber:(NSNumber *)number{
  
  return [self y_formatterNumber:number fractionDigits:2];
}

+ (NSString *)y_formatterNumber:(NSNumber *)number fractionDigits:(NSUInteger)fractionDigits{
    
  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  [numberFormatter setMaximumFractionDigits:fractionDigits];
  [numberFormatter setMinimumFractionDigits:fractionDigits];
  
  return [numberFormatter stringFromNumber:number];
}

加载全部内容

相关教程
猜你喜欢
用户评论