怎么样获得一个小数点后6位的数。
就例如,double c=1.0/(3.0*3.0),本人想要得到一个有效数字为小数点后6位的c。
注意,这里不是要输出格式控制为6位,而是实际的数为小数点后6位。
求高手解答疑惑!~
就例如,double c=1.0/(3.0*3.0),本人想要得到一个有效数字为小数点后6位的c。
注意,这里不是要输出格式控制为6位,而是实际的数为小数点后6位。
求高手解答疑惑!~
解决方案
2
本人写个分数计算
或用大整数当作实数计算
或用大整数当作实数计算
2
#include <stdio.h>
int main() {
const double a = 1.0 / (3.0*3.0);
const double b = a * 1e6;
const int c = (int)b % 10;
printf("%d", c);
return 0;
}
2
有效数字 与 小数点后位数 是两码事
24
需要得到小数点后第n位,之需将原数乘以10的n次方,然后对10取余
20
仅供参考:
#include <stdio.h>
void a(double d,int n) {//有效数字为n位的数
printf("%.*lg\n",n,d);
}
void b(double d,int n) {//小数点后n位的数
printf("%.*lf\n",n,d);
}
int main() {
double c=100.0/(3.0*3.0);
int w;
//有效数字为6位的数
printf("%.6lg\n",c);
//小数点后6位的数
printf("%.6lf\n",c);
printf("\n");
for (w=1;w<15;w+=2) {
a(c,w);
}
printf("\n");
for (w=1;w<15;w+=2) {
b(c,w);
}
return 0;
}
//11.1111
//11.111111
//
//1e+001
//11.1
//11.111
//11.11111
//11.1111111
//11.111111111
//11.11111111111
//
//11.1
//11.111
//11.11111
//11.1111111
//11.111111111
//11.11111111111
//11.1111111111111
//
2
作为一个C程序员,对
scanf,sscanf,fscanf
printf,sprintf,fprintf
这类函数的用法,还是要做到“拳不离手,曲不离口”的。
1
printf(“.6f”,f);就可以了