浮点数的输出讨论
#include<stdio.h>
int main()
{
int count,year;
float interest,rate,sui=0.05,total;
printf("请输入你想存多少的钱:\n");
scanf("%d",&count);
printf("请输入你想存几年:\n");
scanf("%d",&year);
switch(year)
{
case 1:
printf("你想存一年的话rate=1.14那么可以得利息有:\n");
rate=0.0114;
interest=count*rate*year*(1-sui);
break;
case 2:
printf("你想存两年的话rate=2.08可得的利息有:\n ");
rate=0.0208;
interest=count*rate*year*(1-sui);
break;
case 3:
printf("你想存三年的话rate=3.20可得的利息有:\n ");
rate=0.0320;
interest=count*rate*year*(1-sui);
break;
case 4:
printf("你想存四年的话rate=4.15可得的利息有:\n ");
rate=0.0415;
interest=count*rate*year*(1-sui);
break;
}
total=count+interest;
printf("那么你可得的利息有%5.1f",interest);
printf("\n你可得的本金加利息有%-6f",total);
return 0;
}
我输入count为5000存期为两年,最后一行的输出为5197.600098,那么我问题来了我设置的%-6f
宽度的限制作用去哪了?6个宽度理论不应该输出5197.6吗?后面为啥跟那么多零?
而设置%6.1f时候才输出5197.6,如果我设置%1.1f,最后输出也是5197.6,这又是为啥?