Two decimal places using printf( )

前端 未结 4 923
攒了一身酷
攒了一身酷 2020-12-24 10:34

I\'m trying to write a number to two decimal places using printf() as follows:

#include 
int main()
{
  printf(\"When this number:         


        
4条回答
  •  伪装坚强ぢ
    2020-12-24 11:03

    Use: "%.2f" or variations on that.

    See the POSIX spec for an authoritative specification of the printf() format strings. Note that it separates POSIX extras from the core C99 specification. There are some C++ sites which show up in a Google search, but some at least have a dubious reputation, judging from comments seen elsewhere on SO.

    Since you're coding in C++, you should probably be avoiding printf() and its relatives.

提交回复
热议问题