c/c++ float round up

float val = 37.0201;

float rounded_down = floorf(val * 100) / 100;   /* Result: 37.02 */
float nearest = roundf( val * 100) / 100;  /* Result: 37.02 */
float rounded_up = ceilf(val * 100) / 100;      /* Result: 37.03 */

TCHAR number[24]; // dummy size, you should take care of the size!
swprintf_s( number, L "%.2f", rounded_up );
AfxMessageBox( number);

http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c

本文为“技术点滴”的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注