0

你好,我在 C++ 中进行编码,我需要一些帮助来将 double 转换为 int。需要一种从双精度即(3.5945)“3”中获取第一个数字的方法。并将该数字放入一个 int 中。

我现在正在使用 static_cast 并且它返回一个 0。

double X = 3.1234;
double Y = 4.3455;

int myIntX =  static_cast <int>(X);
int myIntY =  static_cast <int>(Y);

cout << myIntX << endl;
cout << myIntY << endl;

输出....

0 0

4

1 回答 1

0

试试这个:

double x=3.1234;
int myintx=(int)x;

while(myintx%10!=0)
myintx/=10;

cout<<myintx;

这将为您提供作为 int 的 double 的第一个数字。

于 2015-12-12T02:54:56.177 回答