问题
Say I have a double as follows:
double aDouble = 15.6;
and I want to convert it to three int's as follows:
int x = 1;
int y = 5;
int z = 6;
How would I go about doing this?
回答1:
double aDouble = 15.6;
int tmp = aDouble*10;
int x, y, z;
x = tmp/100;
tmp -= x * 100;
y = tmp/10;
tmp -= y * 10;
z = tmp;
回答2:
Since this looks like homework, I will give you 2 clues.
- 15.6 = 1 * 10 + 5 * 1 + 6 * 0.1
- casting from a double to an int trucates the double.
You should be able to work out the rest.
来源:https://stackoverflow.com/questions/27304821/how-do-i-separate-digits-from-a-double-and-store-them-as-an-int-in-c