如何将浮点数舍入到小数点后的特定位数?

时间:2012-11-23 03:19:10

标签: c floating-point floating-point-conversion

  

可能重复:
  Is there a function to round a float in C or do I need to write my own?
  Rounding Number to 2 Decimal Places in C

我在c中寻找一个函数,它可以将我的float变量舍入到另一个浮点变量,小数点后面有两位数,请帮助并演示。 谢谢

更新 我需要此功能用于计算,而不是用于打印。

1 个答案:

答案 0 :(得分:4)

你可以尝试这样的事情:

float a = 1.234568;
float b = ((int) a*100.f) / 100.f;

而不是(int)您可以根据自己的要求使用floor()/ceil()