仅在需要时提供精确的数字 - 这可能吗?
是否有可能以不同的精度呈现相同的变量?
例如,如果我要呈现两个整数的加法,它会像:
result = 2+3;
NSLog(@"%.0f", result);
但如果有除法,它会像:
result = 2/3;
NSLog(@"%.1f", result);
虽然呈现 π (pi) 它会像:
result = M_PI;
NSLog(@"%.19f", result); //And it would be 3.141592653589793238
Is there a possibility to present the same variable in different precisions?
For example, if I'm presenting an addition of two integers, it would be like:
result = 2+3;
NSLog(@"%.0f", result);
but with a division it would be like:
result = 2/3;
NSLog(@"%.1f", result);
Although presenting the π (pi) it would be like:
result = M_PI;
NSLog(@"%.19f", result); //And it would be 3.141592653589793238
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSNumber
将为你做一些快速而肮脏的格式化,但你真的应该使用
NSNumberFormatter
:字符串格式说明符也允许可变精度;使用星号代替小数点后的数字:
NSNumber
will do some quick-and-dirty formatting for you,but you should really use an
NSNumberFormatter
:The string format specifiers also allow variable precision; use an asterisk instead of a digit after the decimal point: