狐狸.fox 2018-11-02 01:55 采纳率: 0%
浏览 243

在一定程度上打印输出跟随零

I am trying to print zeros to a certain length to compare with another numbers which is store as string, I was wondering how to do that?

Edit: added sample code
For example:

printf("%s\n", "References:\n3.141592653"
    "58979323846264338327950288419716939937510582097494459230781640628620899");
printf("Monte Carlo: %d nodes over %d iterations calculated Pi as:\n%1.9f\n", numnodes, 
    final_iters,pi);    //Print the calculated value of pi

The out is

References:
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899
Monte Carlo 16 nodes over 160000 iterations calculated Pi as:
3.142025000

How would I make the actual calculation fill in zeros to the end of the string?

转载于:https://stackoverflow.com/questions/53111724/printf-following-zeros-to-a-certain-degree

  • 写回答

2条回答 默认 最新

  • 狐狸.fox 2018-11-02 03:42
    关注

    maybe this will help you. in comment the result, maybe there are better solution but this works. in printf the %016 should be change to your max_buffer length or preferred range and don't forget to add 0 after %.

    printf("%016f\n\n",3.142025000); //// 000000003.142025
    
    
     ///3.14159000000000000000000000000
     #define MAX_BUFFER 32
    float pi = 3.141592653;
    char digit[MAX_BUFFER]={'\0'};
    sprintf(digit, "%f", pi);
    int l = strlen(digit);
    int dif = MAX_BUFFER-l;
     for(int i=0;i<dif;i++)
     {
         digit[i+l-1]='0';
     }
     printf("%s", digit);
    
    评论

报告相同问题?