How to print a double with full precision on iOS?

后端 未结 5 1804
我寻月下人不归
我寻月下人不归 2020-12-17 08:16

Test case:

NSLog(@\"%f\", M_PI);
NSLog(@\"%@\", [NSString stringWithFormat:@\"%f\", M_PI]);
NSLog(@\"%@\", [NSNumber numberWithDouble:M_PI]);
5条回答
  •  佛祖请我去吃肉
    2020-12-17 08:51

    The first two lines round to 6 decimals because that's the default rounding length for printf inherited from C.

    The third line displays the data with the maximum useful precision - an IEEE 754 64bit floating-point number has slightly less than 16 decimal digits of precision, so all those digits of the literal in math.h are pointless (perhaps they can be seen as future-proofing against a possible future redefinition in a format with more precision).

提交回复
热议问题