This question already has answers here:
Can't use modulus on doubles?
                                
                                    (4个答案)
                                
                        
                                9个月前关闭。
            
                    
我是c ++初学者,我想编写一个USD / EUR交换程序,对于输出,我想写:“您得到多少美元/欧分,但是我不知道如何,因为我只需要输出美分。

double eur{0},usd{0},sum{0},kurs{1.14},x,c,d,y;

cout<<"Do you want change Euro or Dollar?"<<endl<<"a is for Euro in Dollar, b is for Dollar in Euro, put a oder b in: "<<endl;

char input = ' ';
cin>>input;
cout<<"Give the sum you want to exchange: ";
cin>>sum;
if (input == 'a'){
    usd=sum;
    x=usd*kurs;
    y= x*100%100; (MODULO is not usable, error, why?)

    cout<<"You get "<< x <<" dollar and " << y << "cents."

最佳答案

您可以使用floor函数获取整数部分并将其从您的数字中减去。

double dollars = floor(input);
double cents = (input - dollars) * 100;

关于c++ - 如何仅输出点号,并在点前切出整数? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55337044/

10-11 22:38
查看更多