I searched around and couldn't find the trunc function for C++. I know I can do this:
int main()
{
    double a = 12.566789;
    cout << setprecision(2) << fixed << (int)(a * 100) / 100.0 << endl;
    return 0;
}
but I'm not sure it's the best way to do this. Thank you.
If your C library is so old that it lacks a trunc function (specified in C99), you can easily implement one based on floor and ceil (specified in C89)
double trunc(double d){ return (d>0) ? floor(d) : ceil(d) ; }
                        trunc is there, in <cmath>:
#include <iostream>
#include <cmath>
int main() {
        std::cout << trunc(3.141516) << std::endl; 
}
I suppose you're looking for something else?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With