iOS NSDecimalNumber精确数值

 

将一个字符串转化为只保留两位小数的浮点数并传回给 服务器

NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];

  NSDecimalNumber *numberrrrra1 = [NSDecimalNumber decimalNumberWithString:self.moneyTF.text];

  NSNumber * numberrra = [numberrrrra1 decimalNumberByRoundingAccordingToBehavior:roundUp];

 

应用场景:

在电商类app中涉及到金钱的充值提现的时候,会对数据的精度有比较高的要求,  有时候直接用 [self.moneyTF.text floatValue ]不太好使,比如 0.1的字符串 经过floatValue转化 会出现0.10000000001的情况

 

参考链接 

https://blog.csdn.net/u010742414/article/details/50186419

https://blog.csdn.net/zx6268476/article/details/45919287

 https://www.jianshu.com/p/c2411302c921

posted on 2015-10-12 22:52  🌞Bob  阅读(331)  评论(0编辑  收藏  举报

导航