How to set number of digits after decimal point for a float value?

without having to use setprecision when printing the variable out? Maybe I don't want to print the variable out, but I still want my program to recognize the value as "10.50000" rather than "10.5". But if I do print it out, it still prints out "10.50000" even without setprecision. How do I do this?
Last edited on
floating point values are stored in IEEE format in bytes/binary.
the program is looking at it in THAT format, not in base 10.
You see what you print, when it converts the unreadable internal format into base 10 and prints it. You can look at it in the debugger, as hex, if you want to see what it really looks like to the computer. Its not going to be very meaningful to you, though.

if you print without setprecision, it prints a default number of decimal places. This is what you are seeing.

10.5 and 10.500000000 are the same number, so is 1.05e1. They are just printed differently for human consumption.

if you need to know how to ensure that it is 10.5 exact and not 10.4999999999999999 or 10.5000000000000000001 that is a different question.
Last edited on
Topic archived. No new replies allowed.