欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-18 19:24:34
1、我们的app有一个判断,当用户输入的值,小于等于剩余的余额时,给用户弹窗,代码不往下执行 当用户输入 0.01 时,po 一下的结果是:0.10000.... ,...

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];
}

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

上一篇:

下一篇: