如何正确传递十进制值作为参数

发布于 2024-07-15 02:39:01 字数 260 浏览 7 评论 0原文

我有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不再让梦枯萎 2024-07-22 02:39:01

atoi() 转换为整数。 您需要 atof()

或者您可以使用 strtod()。

atoi() converts to integer. You want atof().

Or you could use strtod().

悲喜皆因你 2024-07-22 02:39:01

atoi() 转换为整数,您需要 atof(),它转换为双精度数

atoi() converts to an integer, you want atof(), which converts to a double

梦忆晨望 2024-07-22 02:39:01

由于您使用的是 C++,因此还可以使用字符串流:

istringstream ss(arg_vec[1]);
double d;
ss >> d;

Since you're using C++, you can also use stringstreams:

istringstream ss(arg_vec[1]);
double d;
ss >> d;
初心未许 2024-07-22 02:39:01
double d = boost::lexical_cast<double>("0.003");
double d = boost::lexical_cast<double>("0.003");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文