Is there a trunc function in C++?

后端 未结 7 825
情歌与酒
情歌与酒 2021-01-19 04:52

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 <<          


        
7条回答
  •  走了就别回头了
    2021-01-19 05:23

    Sure. Use the trunc() function from math.h. It's a C function, but it works as well in C++ as it does in C. If you want to keep a couple digits, you can always:

    double a = 12.566789;
    double b = trunc(a * 100) / 100.0;
    

提交回复
热议问题