1. ## Formatting number output

Hi everyone,

If I have this code

Code:
```int millisec = 4562
int seconds = (millisec / 1000);```
The output will be 4.562. How can I get the output to be 4.5

Thanks 2. The output will be 4.562. How can I get the output to be 4.5
1. No, there is no output in the snippet posted.

2. No, since both of your variables are integers the answer should be 4. Remember that there are no fractions when doing integer math. 3. Originally Posted by jimblumberg 1. No, there is no output in the snippet posted.

2. No, since both of your variables are integers the answer should be 4. Remember that there are no fractions when doing integer math.
I was using a calculator to get my output as I am trying to make this as simple as possible.

I guess my question should have been how can I take those two numbers and get that specific output. 4. 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';
}``` 5. Thanks John, this definitely helps me out. Popular pages Recent additions code, int, millisec, number, output 