
在C语言中有没有什么样的方法能求出一个小数的小数部分
;例如在输入2.33后,给我输出0.33有谁能帮帮我写下程序谢谢啦我所需要的是可以输入一个数,小数型的,输出这个数的小数部分...
;例如在输入2.33后,给我输出0.33
有谁能帮帮我写下程序
谢谢啦
我所需要的是可以输入一个数,小数型的,输出这个数的小数部分 展开
有谁能帮帮我写下程序
谢谢啦
我所需要的是可以输入一个数,小数型的,输出这个数的小数部分 展开
展开全部
a-=(int)a;
(int)a将a强制转换为正整数,直接舍弃小数部分。
执行上面的a-=(int)a; 得到的a不就是原来的小数部分么?
我说的明白?
(int)a将a强制转换为正整数,直接舍弃小数部分。
执行上面的a-=(int)a; 得到的a不就是原来的小数部分么?
我说的明白?
本回答被提问者采纳

你对这个回答的评价是?
展开全部
先把这个float型的数强制转换为int型,再用原来的float数减去这个int数即可,如:
float a=3.14 ,int b,c;
b=(int)a;
c=a-b;
c即为所求的数
float a=3.14 ,int b,c;
b=(int)a;
c=a-b;
c即为所求的数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
http://www.cplusplus.com/reference/clibrary/cmath/modf.html
double modf ( long double x, long double * intpart );
long double modf ( long double x, long double * intpart );
float modf ( float x, float * intpart ); <cmath>
Break into fractional and integral parts
Breaks x into two parts: the integer part (stored in the object pointed by intpart) and the fractional part (returned by the function).
Each part has the same sign as x.
Parameters
x
Floating point value.
intpart
Pointer to an object where the integral part is to be stored.
Return Value
The fractional part of x, with the same sign.
Portability
In C, only the double version of this function exists with this name.
Example
/* modf example */
#include <stdio.h>
#include <math.h>
int main ()
{
double param, fractpart, intpart;
param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%lf = %lf + %lf \n", param, intpart, fractpart);
return 0;
}
Output:
3.141593 = 3.000000 + 0.141593
double modf ( long double x, long double * intpart );
long double modf ( long double x, long double * intpart );
float modf ( float x, float * intpart ); <cmath>
Break into fractional and integral parts
Breaks x into two parts: the integer part (stored in the object pointed by intpart) and the fractional part (returned by the function).
Each part has the same sign as x.
Parameters
x
Floating point value.
intpart
Pointer to an object where the integral part is to be stored.
Return Value
The fractional part of x, with the same sign.
Portability
In C, only the double version of this function exists with this name.
Example
/* modf example */
#include <stdio.h>
#include <math.h>
int main ()
{
double param, fractpart, intpart;
param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%lf = %lf + %lf \n", param, intpart, fractpart);
return 0;
}
Output:
3.141593 = 3.000000 + 0.141593
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
a = a - int(a);
a是float没错啊
a是float没错啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询