Whenever you divide two integers, the result of this expression also is an integer. Integer division is always rounded down.
newFileTime[i] = fileTime[i] / 10000000;
// ^^^^^^^^^^^ ^^^^^^^^
// int int
To fix your expression, cast one of the operands to a double
to make the division a floating point division (use one of the following possibilities):
newFileTime[i] = static_cast<double>(fileTime[i]) / 10000000;
// or
newFileTime[i] = fileTime[i] / 10000000.0;
Please note that setting the precission requires you to put the call of std::setprecision(...)
into the stream for which you want to set the precision. Also, this only sets the output precision (when writing to the stream), not how the calculations are performed:
std::cout << std::setprecision(4) << [some double values]
Also note that std::setprecision
sets the number of magnificant digets rather than the decimals after the decimal point. So your 302.1680
would be printed as 302.2
(four maginificant digits). To set a fixed number of digits after the decimal point, also write std::fixed
to the stream, either before or after std::setprecision
:
std::cout << std::fixed << std::setprecision(4) << [some double values]
Note that such configurations will be kept during the runtime of your program until you change them again. To keep them local in a function, make sure that you restore the configuration after you're done.
Of course, std::cout
was only an exemplary stream. The same applies for writing to files (any std::ostream
object).