我试图弄清楚如何将剩余部分存储到一个整数之后。
例如:
int amount = 274
int numDollars = amount/100;
//this returns 2 for numDollars, but clears out the .74, and I would like to store 74 to amount
这看起来有可能吗?
答案 0 :(得分:5)
如果不引入新的运算符,您可以从金额中减去美元:
int amount = 274;
int dollars = amount / 100;
int cents = amount - (dollars * 100);
然而,Java,如C和C ++,有一个" modulo" (" mod")运算符给出余数:
int amount = 274;
int dollars = amount / 100;
int cents = amount % 100;
答案 1 :(得分:0)
您可以使用刚刚计算过的数字(numDollars
):
int amount = 274;
int numDollars = amount / 100;
amount = amount - numDollars * 100;
请注意,此行等同于:
amount -= numDollars * 100;