iOS中的精度問題

在iOS中,常常會遇到精度失真的狀況,下面總結一些經常使用的防止轉換失真的方法。spa

 1. double轉number精度失真orm

  double money = 89.99;ci

    NSNumber *moneyNum = [NSNumber numberWithDouble:money];string

    NSLog(@"%@",moneyNum); //此時輸出89.9899999999999;it

  //使用NSDecimalNumber方法

    moneyNum = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",money]];im

    NSLog(@"%@",moneyNum);  //此時輸出89.99總結

2.co

相關文章
相關標籤/搜索