xcode 中的双重问题或我做错了什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3717038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:44:39  来源:igfitidea点击:

double problem in xcode or me doing something wrong

iphoneobjective-cxcodevariablesdouble

提问by Tris

I would guess it's the simplest thing but it's really confusing me. I'm sure I've successfully used doubles before but now I'm having trouble.

我想这是最简单的事情,但它真的让我感到困惑。我确定我以前成功地使用过双打,但现在我遇到了麻烦。

I just made a new 'test' project to see if I can get it working, but all I'm trying to do is set a double value.

我刚刚做了一个新的“测试”项目,看看我是否可以让它工作,但我要做的就是设置一个双倍值。

So in the View Controller's viewDidLoad i've typed:

所以在视图控制器的 viewDidLoad 中,我输入了:

double z = 2938.09;
NSLog(@"z = %d", z);

I would expect it to output 'z = 2938.09' but instead I get 'z = 343597384'

我希望它输出 'z = 2938.09' 但我得到的是 'z = 343597384'

double z = 3.4 returns z = 858993459

双 z = 3.4 返回 z = 858993459

Also most integer values report back as z = 0, however not always (sometimes another strange number as above is spewed out)

此外,大多数整数值报告为 z = 0,但并非总是如此(有时会喷出另一个奇怪的数字)

Am I missing something here or it something strange going on??

我在这里遗漏了什么还是发生了一些奇怪的事情??

Even tried stuff like

甚至尝试过类似的东西

NSString *newString = [[NSString alloc] initWithString:@"3.4"];
double z = [newString.text doubleValue];
NSLog(@"z = %d", z);
[newString release];

but still get the crazy z = 858993459 :(

但仍然疯狂 z = 858993459 :(

回答by Warrior

For printing the Double value use %f instead of %d.

要打印 Double 值,请使用 %f 而不是 %d。

NSlog format specifiers

NSlog 格式说明符

%@ Object

%@ 目的

%d, %i signed int

%d, %i 有符号整数

%u unsigned

%u 未签名

%f float/double

%f 浮点数/双倍数

%x, %X hexadecimal int

%x, %X 十六进制整数

%o octal

%o 八进制

%zu size_t

%zu size_t

%p pointer

%p 指针

%e float/double (in scientific notation)

%e 浮点数/双精度数(科学计数法)

%g float/double (as %f or %e, depending on value)

%g 浮点/双精度(如 %f 或 %e,取决于值)

%s C string (bytes)

%s C 字符串(字节)

%S C string (unichar)

%SC 字符串(单字符)

%.*s Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)

%.*s Pascal 字符串(需要两个参数,第一个传递 pstr[0],第二个传递 pstr+1)

%c character

%c 字符

%C unichar

%C 单字符

%lld long long

%lld 长

%llu unsigned long long

%llu 无符号长长

%Lf long double

%Lf 长双

回答by vfn

Double is a double precision float, and so, to print you should use the same way used to print floats:

Double 是双精度浮点数,因此,要打印您应该使用与打印浮点数相同的方式:

NSString *newString = [[NSString alloc] initWithString:@"3.4"];
double z = [newString.text doubleValue];
NSLog(@"z = %f", z);
[newString release];

%dis used to print signed int.

%d用于打印signed int。

Have a look at this link with the format specifiers for NSLog: http://www.cocoadev.com/index.pl?NSLog

看看这个带有 NSLog 格式说明符的链接:http: //www.cocoadev.com/index.pl?NSLog