C/C++ counting the number of decimals?

后端 未结 13 1952
北荒
北荒 2020-12-06 16:04

Lets say that input from the user is a decimal number, ex. 5.2155 (having 4 decimal digits). It can be stored freely (int,double) etc.

Is there any

13条回答
  •  抹茶落季
    2020-12-06 16:58

    char* fractpart(double f)
     {
        int intary={1,2,3,4,5,6,7,8,9,0};
        char charary={'1','2','3','4','5','6','7','8','9','0'};
        int count=0,x,y;
        f=f-(int)f;
               while(f<=1)
               {
                         f=f*10;
                         for(y=0;y<10;y++)
                             {
                                    if((int)f==intary[y])
                                     {
                                               chrstr[count]=charary[y];
                                               break;
                                     }
                             }
        f=f-(int)f;
        if(f<=0.01 || count==4)
       break;
       if(f<0)
       f=-f;
       count++;
        }
         return(chrstr);
        }
    

提交回复
热议问题