复制代码 在这个方法中,将浮点数num强制转换为整型,以获取整数部分。然后,将整数部分从原始浮点数中减去,以获取小数部分。 使用math.h头文件中的modf函数: #include <stdio.h> #include <math.h> int main() { float num = 3.14; double integer_part; double decimal_part = modf(num, &integer_part);...
#include <stdio.h>voidsplitfloat(floatx,int*intpart,float*fracpart){*intpart = (int) x;*fracpart = x - *intpart; }intmain(void){floatnumber =1.662;inta;floatb; splitfloat(number,&a, &b); printf("intpart = %d, fracpart = %f", a, b);return0; } 1. 2. 3. 4. 5. 6. 7. ...
#include <stdio.h>voidsplitfloat(floatx,int*intpart,float*fracpart){*intpart = (int) x;*fracpart = x - *intpart; }intmain(void){floatnumber =1.662;inta;floatb; splitfloat(number,&a, &b); printf("intpart = %d, fracpart = %f", a, b);return0; } 1. 2. 3. 4. 5. 6. 7. ...