How do I separate digits from a double and store t

2019-09-07 15:35发布

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?

标签: c embedded
2条回答
Summer. ? 凉城
2楼-- · 2019-09-07 15:46
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;
查看更多
狗以群分
3楼-- · 2019-09-07 15:55

Since this looks like homework, I will give you 2 clues.

  1. 15.6 = 1 * 10 + 5 * 1 + 6 * 0.1
  2. casting from a double to an int trucates the double.

You should be able to work out the rest.

查看更多
登录 后发表回答