简体   繁体   English

如何用科学记数法表示NSNumber?

[英]How to represent NSNumber with scientific notation?

I use [NSNumber numberWithDouble:] method to form my NSNumber. 我使用[NSNumber numberWithDouble:]方法形成我的NSNumber。 However, since some of the values might be large, I want to know if there is a way to tell NSNumber to represent itself with the scientific notation. 但是,由于某些值可能很大,因此我想知道是否有一种方法可以告诉NSNumber用科学计数法表示自己。 Thanks in advance! 提前致谢!

Take a look at NSDecimalNumber . 看一下NSDecimalNumber They allow for higher precision than NSNumber , and support scientific notation and other features. 它们允许比NSNumber更高的精度,并支持科学计数法和其他功能。

Try turning the number into as string and use the scientific print formatter %e , or %E 尝试将数字转换为字符串,然后使用科学的打印格式化程序 %e%E

I say this because there is no point in trying to get the NSNumber to format itself because it is only an object wrapper around a value. 我之所以这样说,是因为试图让NSNumber格式化本身没有意义,因为它只是一个围绕值的对象包装器。 When you want to present the number, then treat it as a string and use the NSString methods to show the value in the way that you want. 当您要显示数字时,请将其视为字符串,然后使用NSString方法以所需的方式显示该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM