For example:

double a = 2.12645; std::cout.precision(2); std::cout.setf(std::ios_base::fixed); std::cout << a; 

It rounds and prints 2.13. How to make it so that it does not round off and display 2.12.

    1 answer 1

    If you need non-standard rounding (i.e. clipping), then there are two options - either immediately prepare a number for this (something like a = floor(a*100)/100; or write your own formatter.

    • And then there is boost/format.hpp . Shuffle here . - KoVadim
    • Does boost :: format allow for rounding up? If yes and you know how, throw a reference. - skegg
    • An amendment is not a rounding. This is the bottom cut. Boost can do a lot. But if there is it - I did not see. But for this case, it is probably better to create your own function, which will simply output correctly formatted. But another idea came (need to check). If the number of the digit is removed from the number, then the standard rounding will work correctly. That is, the last line should be replaced by std :: cout << (a - 0.005); But I’ll say right away that this will not always work - in some cases tricky rounding is applied. 2.5 -> 2, 3.5 -> 4, 4.5 -> 4. - KoVadim
    • @KoVadim for case a > 0 ? - alexlz
    • and for negative ones - add. - KoVadim