在C语言中出现问题,将数字四舍五入 [英] Got a problem in the C language with it rounding numbers up

查看:332
本文介绍了在C语言中出现问题,将数字四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,这就是问题所在:我想打印只有三位数的0.3228并使用%。3f但是使用它将它四舍五入到0.323但是我希望它显示0.322任何人都有任何解决方案吗? />


我尝试了什么:



i不知道如何做到这一点我我想在0.3228'2'之后切割数字,但有没有人对此有任何想法

解决方案

  #include   <   stdio.h  >  
#include < math.h >
int main()
{
double d = 0 3228 ;
double truncated = floor(d * 1000 )/ 1000;
printf( d =%。3f,truncated =%。3f\ n ,d,截断);
return 0 ;
}


hey, here is the problem: i wanna print only three digits of 0.3228 and i use "%.3f" but by using this it rounds it up to 0.323 but i want it to show 0.322 anyone got any solutions?

What I have tried:

i have no clue how to do this i was thinking cutting the digits after '2' in 0.3228 but does anyone have any ideas about this

解决方案

#include <stdio.h>
#include <math.h>
int main()
{
  double d = 0.3228;
  double truncated = floor(d*1000)/1000;
  printf("d = %.3f, truncated=%.3f\n", d, truncated);
  return 0;
}


这篇关于在C语言中出现问题,将数字四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆