Formatting Output

How do you get the proper output of a variable, to only one decimal point, and not have the fractal number rounded. Also I would like to do this only using cout function. Not the printf function.

I have tried
cout << setprecision(1) << fixed << "";
The problem is that if the number is 2.49 then you will get 2.5.
I would like for the precision to ignore the .09 and just return 2.4

cout << setprecision(2) << "";
This return the required results however when the number range get higher the
precision will then bump the decimal.

Any help would be awesome
cout << fixed << setprecision(1)

You can also round the number down using floor().
Last edited on
The floor function round it down to the largest integer smaller than the variable.

floor (5.67) becomes 5. I think that won't help you because you need to set the precision to 1.

Next time, please use code tags.
Thank you, sorry new to the site just got on here.

I was informed the it would be ok if rounded. However after 4 hours of research and nothing to help me I would still like to know. Just to inexperienced to get it just yet, But I will lol.

The floor didn't help so far, I looked up a bit on truncation, it would seem that I would
have to write the function myself and we haven't touched on functions so they are not yet allowed.

This is a lesson on conditional if statements and switch statements so i am only allowed to use what i have learned.

Last edited on
The floor didn't help so far, I looked up a bit on truncation, it would seem that I would
have to write the function myself and we haven't touched on functions so they are not yet allowed.


You don't need to write the function yourself, you are just using the function floor(), is that not allowed ?
Last edited on
Topic archived. No new replies allowed.