Is there a trunc function in C++?

后端 未结 7 823
情歌与酒
情歌与酒 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:35

    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) ; }
    

提交回复
热议问题