With <iomanip>
, you can use std::fixed
and std::setprecision
Here is an example
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << d;
}
And you will get output
122.34
More Related Contents:
- Accessing private members of a class from another class
- What is this weird colon-member (” : “) syntax in the constructor?
- When were the ‘and’ and ‘or’ alternative tokens introduced in C++?
- What is the meaning of a const at end of a member function? [duplicate]
- Why can’t the switch statement be applied on strings?
- How to execute a functor or a lambda in a given thread in Qt, GCD-style?
- A better way to avoid public member invisibility and source code bloat/repetition with inherited class templates?
- What is the value of an undefined constant used in #if?
- GCC style weak linking in Visual Studio?
- c++, usleep() is obsolete, workarounds for Windows/MingW?
- GCC C++ “Hello World” program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?
- When should I use raw pointers over smart pointers?
- Why doesn’t my template accept an initializer list
- Retrieving file descriptor from a std::fstream [duplicate]
- rand() generating the same number – even with srand(time(NULL)) in my main!
- Are C++11 thread_local variables automatically static?
- How do I tell if the c function atoi failed or if it was a string of zeros?
- Can std::launder be used to convert an object pointer to its enclosing array pointer?
- C++: overriding public\private inheritance
- How can I avoid including class implementation files?
- Creating a new C++ Project in Eclipse CDT with the same settings as another project
- Template typedefs – What’s your work around?
- Are there in x86 any instructions to accelerate SHA (SHA1/2/256/512) encoding?
- Call destructor and then constructor (resetting an object)
- STL deque accessing by index is O(1)?
- How to read huge file in c++
- Is MAXIMUM_WAIT_OBJECTS really 64?
- Convert inline assembly code to C++
- Why can’t std::function bind to C-style variadic functions?
- if constexpr – why is discarded statement fully checked?