One way is to use a floating point variable (double is the common choice) and set cout to print a fixed number of fractional digits with that number (precision) being 1:
Code:
#include <iostream>
#include <iomanip>
int main()
{
int millisec = 4562;
// Must be floating point to have a fractional part.
// Note that the calculation must have at least one floating point value
// (1000.0 in this case) for the calculation to be floating point.
double seconds = (millisec / 1000.0);
// Set cout to a fixed number of fractional digits
// In fixed mode, the precision indicates the number of fractional digits.
std::cout << std::fixed << std::setprecision(1);
std::cout << seconds << '\n'; // prints 4.6
}
Note that this rounds up.
Alternatively you can do it with just integer math:
Code:
#include <iostream>
int main()
{
int millisec = 4562;
// Without rounding (prints 4.5)
std::cout << millisec / 1000 << '.' << millisec % 1000 / 100 << '\n';
// With rounding (prints 4.6)
std::cout << millisec / 1000 << '.' << (millisec + 50) % 1000 / 100 << '\n';
}