What you're looking for is truncation. This should work (at least for numbers that aren't terribly large):
printf(".2f", ((int)(100 * var)) / 100.0);
The conversion to integer truncates the fractional part.
In C++11 or C99, you can use the dedicated function trunc
for this purpose (from the header <cmath>
or <math.h>
. This will avoid the restriction to values that fit into an integral type.
std::trunc(100 * var) / 100 // no need for casts
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…