[求解] 只想把数值(保留小数点后两位),存到变数里(不是用printf)
例如:float kt = 40.536738;
希望把它变成 40.54 再存到变数中,
即:a的值为40.54
float kt = 40.536738; int a=(int)(kt*1000); float b=a/10.0; int c=(int)(b+0.5); float d=c/100.0;
#include<stdio.h> #include<stdlib.h> int main() { float kt = 40.536738; char* s = NULL; sprintf(s,"%.2f\n",kt); printf("%d\n", s); float target = atof(s); printf("%f\n", target); return 0; }
结果是: 0 0.000000
[此贴子已经被作者于2017-7-26 10:23编辑过]