cocoa - 将double转换为string

Spa*_*Dog 9 iphone cocoa iphone-sdk-3.0

我有一个双号,我想将其转换为字符串.

例如,这个数字就像是

24.043333332154465777 ...

但如果我使用类似的东西将其转换为字符串

NSString *myString = [NSString stringWithFormat:@"%f", myDouble];
Run Code Online (Sandbox Code Playgroud)

字符串就是

24.043333

如何获得与整个双号对应的完整字符串?我有什么其他方法来转换它?

Jas*_*ien 40

另一种选择,因为你在评论mipadi的答案时要求其他方式:

创建一个NSNumber使用NSNumber *myDoubleNumber = [NSNumber numberWithDouble:myDouble];

然后打电话 [myDoubleNumber stringValue];

来自文档:

将接收者的值作为人类可读的字符串返回,通过调用descriptionWithLocale创建:其中locale为nil.


Haf*_*hor 21

[NSString stringWithFormat:@"%.20f", myDouble];
Run Code Online (Sandbox Code Playgroud)

要么

@(myDouble).stringValue;
Run Code Online (Sandbox Code Playgroud)


mip*_*adi 12

您可以将宽度格式说明符传递给stringWithFormat.

NSString *myString = [NSString stringWithFormat:@"%.20f", myDouble];
Run Code Online (Sandbox Code Playgroud)

将格式化myDouble为20位小数.