There is some kind of function for the printf function in which you can use %g, which will show the whole number 3
if the float is 3.00 and will show 3.01
if it's actually a float, is there any way you can do this through some code?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Do the Java Integer and Double objects have unnece
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
There isn't really a simple answer
Integral values do have exact representations in the float and double formats. So, if it's really already integral, you can use:
However, if your value is the result of a calculation which at one point involved any sort of non-zero fractional part, then you will need to be concerned that you may have something very close to an integer but which isn't really, exactly, to-the-last-bit the same. You probably want to consider that to be integral.
One way this might be done:
And while we are on the subject, for the purists, we should note that
int i = f;
ordouble i = f;
will round according to the FPU mode whereas round(3) will round half-way cases away from zero.Try double modf( double, double * ) function. [http://www.codecogs.com/reference/computing/c/math.h/modf.php][1]