在项目中经常使用到货币的结算问题,但是货币的精度问题真的很让人头疼~~不论你是用float类型还是double类型在累加的时候好像总是精度不够呢

  • 这里我们就使用到了NSDecimalNumber货币类了,初始化对象
    NSDecimalNumber *decimal = [[NSDecimalNumber alloc]initWithString:string];
  • 价格的累加,decimalNumberByAdding:。货币累的累加并不能像其他的基本数据类型似的 +=或者 ++,这里的计算只能使用一个NSDecimalNumber类型的对象承接上一个对象的数据。
    NSDecimalNumber *defaultPrice = [self getDecimalPriceWithNum:@"0"];
    NSDecimalNumber *totalPrice = [self getDecimalPriceWithNum:@"0"];
    //计算总价 确保精度
    NSDecimalNumber *getPrice = [self getDecimalPriceWithNum:[NSString stringWithFormat:@"%@",price]];
    defaultPrice = totalPrice;
    totalPrice = [defaultPrice decimalNumberByAdding:getPrice];

    解决货币精度问题(NSDecimalNumber)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2021-12-02
  • 2021-07-24
  • 2021-06-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-03-09
  • 2021-10-10
  • 2022-01-21
相关资源
相似解决方案