C语言 a*10%10问题

来源:百度知道 编辑:UC知道 时间:2024/05/13 03:29:43
a(如:3.65)是一个实数,我想把它第一位小数取出来
用a*10%10
怎么会出错 怎么样改正 能取出第一位小数

浮点数不能做%运算

((int)(a*10))%10

(int(a*10))%10

呵呵,当然不能这样做啦, %操作符要求操作数是整数,如果要达到你的目的,可以使用floor()函数,它在math.h中声明,作用是返回小于等于传入参数小的最大整数。

//---------------------------------------------------------------------------
#include <math.h>
#include <stdio.h>

int main(void)
{
float x=6.36;
x*=10;
printf("%.2lf",x-floor(x));
return 0;
}
//---------------------------------------------------------------------------

#include "stdio.h"
main()
{
float a=3.65;
int b,c;
b=a;
c=a*10-b*10;
printf("%f",(float)c/10);
getch( );
}