查看: 205|回复: 0

[IOS开发教程] 详解iOS之关于double/float数据计算精度问题

发表于 6 天前

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 类型的对象进行 +、-、*、/计算,再取值。

  1. - (double)DecimalNumber:(double)num1 num2:(double)num2 {
  2. NSDecimalNumber *n1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",num1]];
  3. NSDecimalNumber *n2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",num2]];
  4. NSDecimalNumber *n3 = [n1 decimalNumberBySubtracting:n2];
  5. return n3.doubleValue;
  6. }
  7. // 调用:
  8. 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、进行数据的四舍五入获得结果

  1. - (NSString*)Rounding:(float)number afterPoint:(NSInteger)position
  2. {
  3. NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode: NSRoundPlain scale: position raiseOnExactness: NO raiseOnOverflow: NO raiseOnUnderflow:NO raiseOnDivideByZero: NO];
  4. NSDecimalNumber *floatDecimal = [[NSDecimalNumber alloc] initWithFloat: number];
  5. NSDecimalNumber *resultNumber = [floatDecimal decimalNumberByRoundingAccordingToBehavior:handler];
  6. return [NSString stringWithFormat:@"%@",resultNumber];
  7. }
  8. // 调用:
  9. NSString *result = [self Rounding:8.00092 afterPoint:3]; // 结果为 8.001
复制代码

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

高精度要求

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

  1. #import "NSDecimalNumber+Y_Add.h"
  2. @implementation NSDecimalNumber (Y_Add)
  3. + (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value{
  4. return [self y_decimalNumberWithFloat:value scale:2];
  5. }
  6. + (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value scale:(short)scale{
  7. return [self y_decimalNumberWithFloat:value roundingMode:NSRoundBankers scale:scale];
  8. }
  9. + (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value roundingMode:(NSRoundingMode)roundingMode scale:(short)scale{
  10. return [[[NSDecimalNumber alloc] initWithFloat:value] y_decimalNumberHandlerWithRoundingMode:roundingMode scale:scale];
  11. }
  12. + (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value{
  13. return [self y_decimalNumberWithDouble:value scale:2];
  14. }
  15. + (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value scale:(short)scale{
  16. return [self y_decimalNumberWithDouble:value roundingMode:NSRoundBankers scale:scale];
  17. }
  18. + (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value roundingMode:(NSRoundingMode)roundingMode scale:(short)scale{
  19. return [[[NSDecimalNumber alloc] initWithFloat:value] y_decimalNumberHandlerWithRoundingMode:roundingMode scale:scale];
  20. }
  21. /**
  22. * <#Description#>
  23. *
  24. * @return <#return value description#>
  25. */
  26. - (NSDecimalNumber *)y_decimalNumberHandler{
  27. return [self y_decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:2];
  28. }
  29. - (NSDecimalNumber *)y_decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale{
  30. NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:roundingMode
  31. scale:scale
  32. raiseOnExactness:NO
  33. raiseOnOverflow:YES
  34. raiseOnUnderflow:YES
  35. raiseOnDivideByZero:YES];
  36. return [self decimalNumberByRoundingAccordingToBehavior:handler];
  37. }
  38. @end
复制代码

普通精度要求

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

  1. /**
  2. * formatterNumber .00 小数点后两位
  3. *
  4. * @param number <#number description#>
  5. *
  6. * @return <#return value description#>
  7. */
  8. + (NSString *)y_formatterNumber:(NSNumber *)number{
  9. return [self y_formatterNumber:number fractionDigits:2];
  10. }
  11. + (NSString *)y_formatterNumber:(NSNumber *)number fractionDigits:(NSUInteger)fractionDigits{
  12. NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  13. [numberFormatter setMaximumFractionDigits:fractionDigits];
  14. [numberFormatter setMinimumFractionDigits:fractionDigits];
  15. return [numberFormatter stringFromNumber:number];
  16. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报