如何让float变量精确到小数点后两位
2个回答
2017-04-07 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
float变量精确到小数点后两位
比如:
long i = long.MaxValue - 12345678901234L;
float f = i;
double d = i;
Console.WriteLine( "long i : {0:N3} ", i);
Console.WriteLine( "float f = i: {0:N3} ", f);
Console.WriteLine( "(double)f : {0:N3} ", (double)f);
Console.WriteLine( "(long)f : {0:N3} ", (long)f);
Console.WriteLine( "double d = i: {0:N3} ", d);
Console.WriteLine( "(long)d : {0:N3} ", (long)d);
Console.WriteLine( "d - f : {0:N3} ", d - f);
比如:
long i = long.MaxValue - 12345678901234L;
float f = i;
double d = i;
Console.WriteLine( "long i : {0:N3} ", i);
Console.WriteLine( "float f = i: {0:N3} ", f);
Console.WriteLine( "(double)f : {0:N3} ", (double)f);
Console.WriteLine( "(long)f : {0:N3} ", (long)f);
Console.WriteLine( "double d = i: {0:N3} ", d);
Console.WriteLine( "(long)d : {0:N3} ", (long)d);
Console.WriteLine( "d - f : {0:N3} ", d - f);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-04-07
展开全部
float spd = 22.518744;
char buf[10];
sprintf(buf, "%.2f", spd);
sscanf(buf, "%f", &spd);
char buf[10];
sprintf(buf, "%.2f", spd);
sscanf(buf, "%f", &spd);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询