Is there a way to round floating points to 2 points? E.g.: 3576.7675745342556
becomes 3576.76
.
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
To limit the precision:
If x is a float, no rounding:
(shift up by 2 decimal digits, strip the fraction, shift down by 2 decimal digits)
Float w/ rounding:
Double w/o rounding:
Double w/ rounding:
Note: Because x is either a
float
or adouble
, the fractional part will always be there. It is the difference between how a # is represented (IEEE 754) and the #'s precision.C99 supports round()
If you must keep things floats:
Flexible version using standard library functions:
If you are printing it out, instead use whatever print formatting function available to you.
In c++
For rounding to render to GUI, use std::ostringstream