How do I separate digits from a double and store them as an int in C?

谁都会走 提交于 2019-12-14 03:13:43

问题


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.

  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.



来源:https://stackoverflow.com/questions/27304821/how-do-i-separate-digits-from-a-double-and-store-them-as-an-int-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!