Two decimal places using printf( )

前端 未结 4 963
攒了一身酷
攒了一身酷 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:02

    Try using a format like %d.%02d

    int iAmount = 10050;
    printf("The number with fake decimal point is %d.%02d", iAmount/100, iAmount%100);
    

    Another approach is to type cast it to double before printing it using %f like this:

    printf("The number with fake decimal point is %0.2f", (double)(iAmount)/100);
    

    My 2 cents :)

提交回复
热议问题