实现实数的整数与小数部分的拆分
作者:互联网
实现实数的整数与小数部分的拆分
代码如下:
// An highlighted bl`
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void splitfloat( float x, int *intpart, float *fracpart );
void splitfloat( float x, int *intpart, float *fracpart )
{
*intpart = (int)x;
*fracpart = x- (*intpart);
}
int main()
{
float x, fracpart;
int intpart;
scanf("%f", &x);
splitfloat(x, &intpart, &fracpart);
printf("The integer part is %d\n", intpart);
printf("The fractional part is %g\n", fracpart);
return 0;
}
最多可以提取6位小数
但是当输入7.1时,得到的就是7和0.9999,而输入8.1就是正常的8和0.1
借鉴链接: https://blog.csdn.net/Qianzshuo/article/details/103758973?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control
标签:小数,实数,float,blog,int,splitfloat,拆分,intpart,fracpart 来源: https://blog.csdn.net/weixin_46283171/article/details/112101929