Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要将整数和小数部分与小数分开,如 4.343。
我们在 C/C++ 中有没有给出小数点位置的函数。得到之后,我想我们可以使用 atoi 函数转换为数字。
谢谢!
如果4.343是字符串,请查看strchr.
4.343
strchr
#include <string.h> char src[] = "4.343"; char *p = strchr(src, ".");
然后你可以使用atoi如下。
atoi
#include <stdlib.h> if (p != NULL) { *p = '\0'; res = atoi(src); }
您可以使用该floor()函数获取整数部分,然后对于分数只需从原始值中减去整数部分...
floor()
您还可以使用 scanf 或其亲属 fscanf 和 sscanf 提取数据
scanf("%d.%d",&a,&b);
那么 a 的位数等于 .Fraction 的位置,整数部分也被存储以备将来使用