如何正确传递十进制值作为参数
我有这个:
double myDecimal = static_cast<double>(atoi(arg_vec[1]));
cout << myDecimal << endl;
但是为什么当我这样传递参数时:
./MyCode 0.003
它打印 0
而不是 0.003
。
I have this:
double myDecimal = static_cast<double>(atoi(arg_vec[1]));
cout << myDecimal << endl;
But why when I pass the argument like this:
./MyCode 0.003
It prints 0
instead of 0.003
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
atoi()
转换为整数。 您需要atof()
。或者您可以使用 strtod()。
atoi()
converts to integer. You wantatof()
.Or you could use strtod().
atoi() 转换为整数,您需要 atof(),它转换为双精度数
atoi() converts to an integer, you want atof(), which converts to a double
由于您使用的是 C++,因此还可以使用字符串流:
Since you're using C++, you can also use stringstreams: