std::ios_base::precision(3)
π NAME π
std::ios_base::precision β okay so this is literally the function that manages std::ios_base::precision, no cap. It's the one from the C++ Standard Libary manual (yes bestie that's how they spelled it, I'm not fixing it, I don't make the rules π).
β¨ Synopsis β¨
streamsize precision() const; (1)
streamsize precision( streamsize new_precision ); (2)
So picture this: precision is the vibe manager for how many digits show up when you print out floating point stuff. Specifically, it controls the precision (i.e. how many digits are generated) of floating point output performed by std::num_put::do_put. That's the whole job. Slay.
- This overload just returns the current precision. It's a little read-only bestie, it doesn't change anything, it just tells you the tea. π«Ά
- This one sets the precision to the given one. And the cute part? It returns the previous precision β so you get the old number back as a lil souvenir. π
The default precision, as established by std::basic_ios::init, is 6. Six. That's the starting point before you touch anything. Obsessed.
πΈ Parameters πΈ
new_precisionβ the new precision setting. This is the number you're handing over. That's it, that's the parameter.
π¦ Return value π¦
The precision before the call to the function. So it's like, the past version of the precision, returned to you. Time travel but make it integer. β
π₯ Example π₯
// Run this code
#include <iostream>
int main()
{
const double d = 12.345678901234;
std::cout << "The default precision is " << std::cout.precision() << "\n\n";
std::cout << "With default precision d is " << d << '\n';
std::cout.precision(8);
std::cout << "With high precision d is " << d << '\n';
}
π Output
The default precision is 6
With default precision d is 12.3457
With high precision d is 12.345679
See how the first one only gave us 12.3457 and then we bumped it to 8 and suddenly we got 12.345679? Literally glow-up behavior. The precision said "watch this" and ate. π
π Defect reports π
Okay so the following behavior-changing defect reports were applied retroactively to previously published C++ standards. Which means yes, they went back and fixed old stuff, iconic of them honestly:
- DR:
LWG 189β Applied to:C++98β Behavior as published:'precision'was defined as'the number of digits after the decimal point', but it is not correct in some cases β Correct behavior: corrected. π
So yeah, the old wording was lowkey wrong in some cases, and they corrected it. Fixed. Periodt.
π§ See also π§
widthβ manages field width (public member function)setprecisionβ changes floating-point precision (function)
That's the whole page bestie, go forth and format your floats π
Shoutout tohttp://cppreference.com, the2024.06.10crew, and the page itself:std::ios_base::precision(3). Mwah. π β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms