Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed Decimal Precision [duplicate]

Tags:

c++

I am currently using

std::cout.precision(5);

to set the decimal precision of my outputs. However, I would rather have my output ALWAYS output 5 decimal places (right now it won't show 0's). How would I change my code to reflect this?

like image 597
user2105982 Avatar asked Dec 11 '22 15:12

user2105982


1 Answers

You are looking for std::fixed together with std::setprecision.

#include <iomanip>
#include <iostream>
double f =1.1;
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << std::endl;

stdout

1.10000
like image 197
stardust Avatar answered Dec 24 '22 09:12

stardust