Is there a function to round a float or do I have to type my own?
Float Conversion = 45. 59 2346543;
I want to convert the actual value to a decimal place = 45. 6 .
As Rob mentioned, maybe you just want print 1 Float the decimal place In this case, you can do something like:
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Int main () {float conveyor = 45.592346543; Printf ("converter% 0.1f \ n", is converter); Return 0; }
If you really want to round the stored value, then it is a bit more complex. For one, the representation of your one-decimal place would hardly be the exact analog in the floating-point. If you want to walk as close as possible, then something like this can be done:
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Math.h> Int main () {float conveyor = 45.592346543; Printf ("converter% 0.1f \ n", is converter); Converter = converter * 10.0 f; Converter = (converter & gt; (flooring (converter) + 0.5F))? Seat (converter): floor (converter); Converter = converter / 10.0 f; // If you are using CAM or better than ANSI C / C8 / C 90, then the following will also work. // conver = roundf (converter * 10.0 f) / 10.0 f; Printf ("converter is now% F \ n", converter); Return 0; }
I suspect that this is the second example you are looking for, but I had included it for perfection. If you need to show your numbers internally in this way, rather than just output, instead consider using one.
Comments
Post a Comment