Rounding doubles - .5 - sprintf

前端 未结 5 820
遥遥无期
遥遥无期 2021-01-11 20:15

I\'m using the following code for rounding to 2dp:

sprintf(temp,\"%.2f\",coef[i]); //coef[i] returns a double

It successfully rounds 6.666

5条回答
  •  死守一世寂寞
    2021-01-11 20:46

    This question is tagged C++, so I'll proceed under that assumption. Note that the C++ streams will round, unlike the C printf family. All you have to do is provide the precision you want and the streams library will round for you. I'm just throwing that out there in case you don't already have a reason not to use streams.

提交回复
热议问题